当前位置:首页 » 编程语言 » php图片缩放比例缩放

php图片缩放比例缩放

发布时间: 2022-06-11 16:50:05

⑴ 如何缩小图片分辨率 本人有若干张图片要传到网上,想把图片分辨率调小一些有什么软件支持批量转换

批量修改图片分辨率的方法:

步骤1,安装工具软件后打开使用,点击软件左边五个功能中的【更改尺寸】功能,然后再点击【添加文件】按钮将需要修改分辨率的图片添加到软件中。

php 如何将上传的图片按比例缩放并存在服务器里

//接收上传的文件
foreach($_FILESas$file)
{
$tempFileName=$file['tmp_name'];//上传文件的临时路径
}
/把图片移动到服务器制定路径
$img='/var/www/html/picture/test.jpg';
move_uploaded_file($tempFileName,$img);

//缩放比例
$ratio=0.5;

//修改尺寸至于各个函数是干嘛的,google一下吧
$imagedata=getimagesize($img);
$olgWidth=$imagedata[0];
$oldHeight=$imagedata[1];
$newWidth=$olgWidth*$ratio;
$newHeight=$oldHeight*$ratio;

$image=imagecreatefromjpeg($img);
$thumb=imagecreatetruecolor($newWidth,$newHeight);
imageresized($thumb,$image,0,0,0,0,$newWidth,$newHeight,$olgWidth,$oldHeight);
imagejpeg($thumb,$img);

imagedestroy($thumb);
imagedestroy($image);

⑶ php等比例缩放图片原理

这里的$width主要是在$width没有设置的时候直接用$height进行处理(else),在这里意义不大。
if判断除了0/false/null(可能点漏)几个,其他数值、包括负数,都为真。
你的if/else中的核心是谁大按谁的比例进行缩放。

⑷ 如何实现PHP图片裁剪与缩放

给你段代码吧。上边的是具体的事务处理。下面的是类文件。
////////////////////////////////////////////////////下面开始处理图片压缩问题
$src = "$fileurl";

echo $src;
$image = new Image($src);
$width= $image->getimgwidth();
echo $width."宽度是这些";

if($width>1024){

$coefficient=number_format(1024/$width, 4, '.', '');
echo $coefficient;
$image->percent = $coefficient;
$image->openImage();
$image->thumpImage();
//************************************重新给图片命名
$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;

echo "<br/>重新命名是这个".$randname;

$fileurl=$fileimgweb.$randname;//重新给数据库里存的图片地址命名
// $image->showImage();
$image->saveImage($fileurl);

}

////////////////////////////////////////////////////图片压缩问题处理结束

--------------------------------------------------------------------------------------

<?php
/**
图片压缩操作类
v1.0
*/
class Image{

private $src;
private $imageinfo;
private $image;
public $percent = 0.5;
public function __construct($src){

$this->src = $src;

}
/**
获取图片的宽度并传输给前台
*/

public function getimgwidth(){

$imgwidth= getimagesize($this->src)[0];
// echo $imgwidth;
return $imgwidth;

}

/**
打开图片
*/
public function openImage(){

list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(

'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);

}
/**
操作图片
*/
public function thumpImage(){

$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
imageresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
输出图片
*/
public function showImage(){

header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);

}
/**
保存图片到硬盘
*/
public function saveImage($fileurl){

imagejpeg($this->image, $fileurl,75);

// $funcs = "image".$this->imageinfo['type'];
// $funcs($this->image,$name.'.'.$this->imageinfo['type']);

}
/**
销毁图片
*/
public function destruct(){

imagedestroy($this->image);
}

}

?>

⑸ 如何实现php图片等比例缩放

$imgsrc=$image_name;
$imgdst=$image_name;
list($width,$height,$type)=getimagesize($imgsrc);
$new_width=($width>600?600:$width)*0.9;
$new_height=($height>600?600:$height)*0.9;
$giftype=check_gifcartoon($imgsrc);
$image_wp=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromgif($imgsrc);
imageresampled($image_wp,$image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($image_wp,$imgdst,75);
imagedestroy($image_wp);

⑹ php实现图片等比例缩放代码

新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称)
源代码如下:
<?php
$filename="q.jpg";
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整
imageresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//图像输出新图片、另存为
imagejpeg($new,
"q1.jpg");
imagedestroy($new);
imagedestroy($img);
?>
使用浏览器运行过后,在index.php同级的目录下会有个q1.jpg,这个图片就是等比例缩放后的图片,路径可以自己在源代码里面更改,放在自己的项目当中去或写个方法也行
以上所述上就是本文的全部内容了,希望对大家学习php语言能够有所帮助。

⑺ php 怎么实现不等比例缩放图片

functiontep_image($src,$alt='',$width='',$height='',$parameters=''){

$image='<imgsrc="'.tep_output_string($src).'"alt="'.$alt.'"';

if(tep_not_null($alt)){
$image.='title="'.$alt.'"';
}

if(((empty($width)||empty($height))){
if($image_size=@getimagesize($src)){
if(empty($width)&&tep_not_null($height)){
$ratio=$height/$image_size[1];
$width=intval($image_size[0]*$ratio);
}elseif(tep_not_null($width)&&empty($height)){
$ratio=$width/$image_size[0];
$height=intval($image_size[1]*$ratio);
}elseif(empty($width)&&empty($height)){
$width=$image_size[0];
$height=$image_size[1];
}
}
}

if(tep_not_null($width)&&tep_not_null($height)){
$image.='width="'.$width.'"height="'.$height.'"';
}

if(tep_not_null($parameters))$image.=''.$parameters;

$image.='/>';

return$image;
}
functiontep_not_null($value){
if(is_array($value)){
if(sizeof($value)>0){
returntrue;
}else{
returnfalse;
}
}else{
if(($value!='')&&(strtolower($value)!='null')&&(strlen(trim($value))>0)){
returntrue;
}else{
returnfalse;
}
}
}

用tep_image('图片路径','图片alt','width','height');

宽高自己设定 就可以自动等比例缩放了

⑻ PHP等比例压缩图片的实例代码

具体代码如下所示:
/**
*
desription
压缩图片
*
@param
sting
$imgsrc
图片路径
*
@param
string
$imgdst
压缩后保存路径
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//压缩后的图片宽
$new_height
=
$height;//压缩后的图片高
if($width
>=
600){
$per
=
600
/
$width;//计算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
总结
以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP
实现等比压缩图片尺寸和大小实例代码php
gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例

⑼ php如何实时缩小图片大小

PHP中缩放图像:

有两种改变图像大小的方法.

(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.

(2):ImageCopyResampled(),其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比
ImageCopyResized() 慢).

两个函数的参数是一样的.如下:

ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

它们两个都是从原图像(source)中抓取特定位置(sx,sy)复制图像qu区域到目标t
图像(destination)的特定位置(dx,dy)。另外dw,dh指定复制的图像区域在目标图像上的大小,sw,sh指定从原图像复制的图像区域
的大小。如果有ps经验的话,就相当于在原图像选择一块区域,剪切移动到目的图像上,同时有拉伸或缩小的操作。

例一:

(本例子是将图片按原大小的4/1的大小显示)

<?php
// 指定文件路径和缩放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定头文件Content type值
header('Content-type: image/jpeg');
// 获取图片的宽高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = imagecreatefromjpeg($filename);
// 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
imageresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 输出给浏览器
imagejpeg($thumb);
?>

⑽ 跪求PHP上传图片并按比例缩放到一定尺寸的程序,非常感谢

publicfunctionupimges(){
$filename=time();
header("Content-Type:text/html;charset=utf-8");
$upload=newUpimage('','','','',$filename);
//设置上传文件大小
$upload->maxSize=100000000000000;
//$upload->saveRule=microtime().'_'.mt_rand(1000,9999);
//上传文件命名规则timeuniqidcom_create_guid等
//$upload->$saveRule=time();
//设置上传文件类型
$upload->allowExts=explode(',','jpg,gif,png,jpeg');
//设置附件上传目录
$upload->savePath='./data/attached/skp/'.$_SESSION['formtoken'].'/';
$upload->thumb=true;
//设置引用图片类库包路径
$upload->imageClassPath='./Image.php';
//设置需要生成缩略图的文件后缀
$upload->thumbPrefix='thumb_,thumbm_,thumbl_,thumbs_';//生产4张缩略图
//设置缩略图最大宽度
$upload->thumbMaxWidth='960,480,120,64';
//设置缩略图最大高度
$upload->thumbMaxHeight='960,480,120,64';
//删除原图
$upload->thumbRemoveOrigin=false;

if(!$upload->upload()){
$data['tip']=$upload->getErrorMsg();
$data['status']='0';
}else{
$info=$upload->getUploadFileInfo();

$data['counts']=count($info);
$data['status']='1';
$data['tip']='上传成功';
$data['info']=$info;
}

echojson_encode($data);
}

这个是代码和所引用的

热点内容
bin存储 发布:2025-02-07 20:00:50 浏览:202
android加载界面 发布:2025-02-07 19:55:28 浏览:870
好矿云服务器 发布:2025-02-07 19:54:31 浏览:948
java电话簿 发布:2025-02-07 19:49:26 浏览:796
超级脚本制作 发布:2025-02-07 19:31:30 浏览:486
怎么查看支付宝的账号密码 发布:2025-02-07 19:26:48 浏览:16
惠普服务器查看ip指令 发布:2025-02-07 19:26:47 浏览:434
算法设计模式 发布:2025-02-07 19:15:52 浏览:746
服务器1u能连接几台电脑 发布:2025-02-07 18:50:02 浏览:154
立人编译 发布:2025-02-07 18:48:32 浏览:766