php等比例缩放图片
‘壹’ php等比缩放图片,就是只按宽度缩小图片,当图片宽度大于750时就缩小到750 高度不用管 跟着宽度缩就行了
首先说一下思路,首先你要判断图片的宽度,这需要用到一个函数,个人比较喜欢用getimagesize()
其次是等比例绽放,需要用到imageresized(当然还有其他函数)
注意:我这里用到的是gd库
实现:
写一个函数或者类都行,我这里就以面向过程的方式来写,你可以整理一下
$file = 'pic.jpg'; //原图片文件
$maxWidth = 750;
$info = getimagesize($file); //取得一个图片信息的数组,索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记
if($info[0] > $maxWidth )
{
exit('图片小于'.$maxWidth.',不需要缩放');
}
$im = imagecreatefromjpeg($file); //根据图片的格式对应的不同的函数,在此不多赘述。
$rate = $maxWidth/$info[0]; //计算绽放比例
$maxHeight = floor($info[1]*$rate); //计算出缩放后的高度
$des_im = imagecreatetruecolor($maxWidth,$maxHeight); //创建一个缩放的画布
imageresized($des_im,$im,0,0,0,0,$maxWidth,$maxHeight,$info[0],$info[1]); //缩放
imagejpeg($des_im,'thumb.jpg'); //输出到thumb.jpg即为一个缩放后的文件
‘贰’ 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等比例压缩图片的实例代码
具体代码如下所示:
/**
*
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等比例缩放图片原理
这里的$width主要是在$width没有设置的时候直接用$height进行处理(else),在这里意义不大。
if判断除了0/false/null(可能点漏)几个,其他数值、包括负数,都为真。
你的if/else中的核心是谁大按谁的比例进行缩放。
‘伍’ PHP中缩放图像的问题
是不是。。就是额外拷贝一份。。。
“缩小图像“应该只是图像参数的变化吧。。。“白色的图片“可能先要连接到你的图像src,然后根据实时参数进行相同的调整。。。
----------------------------------------------------------------------
1."方框中的任意部分,就可以进入"----可不可以用<area></area>
2.缩放的图片可不可以作为一个layer覆盖在方框上,实现layer的话就比如z-index等于大于0的值
3.当你点击方框的时候。。callback点击缩放的图片。。。。。
-------------------------------------------------------
晕。。。真的呀!!!!
我想是3个办法的。。可能第三个最practice。。。。。
就是你不是有方框嘛。。你的方框的触发函数--比如onclick转而去调用你的“缩小图像“的对应的事件--我觉得正常可能也是onclick。。。 有点callback的味道。。。callback就是我们说的回调。。。
方框说:你敢点我。。我就点“缩小图像“
是吧。。。你觉得呢。。。。
‘陆’ 如何实现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图片裁剪与缩放
给你段代码吧。上边的是具体的事务处理。下面的是类文件。
////////////////////////////////////////////////////下面开始处理图片压缩问题
$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图片缩放处理函数
<?php
/**
*图片缩放
*@paramstring$url
*@paramint$maxWidth
*@paramint$maxHeight
*@returnstring
*/
functionthumb($url,$maxWidth,$maxHeight,&$info){
$info=$imgInfo=getimagesize($url);
$width=$imgInfo[0];//获取图片宽度
$height=$imgInfo[1];//获取图片高度
$r=min($maxHeight/$height,$maxWidth/$width);
if($r>=1){//不用缩放
$maxHeight=$height;
$maxWidth=$width;
}elseif($r<1){//缩放
$maxHeight=$height*$r;
$maxWidth=$width*$r;
}
$temp_img=imagecreatetruecolor($maxWidth,$maxHeight);//创建画布
$fun=str_replace('/','createfrom',$imgInfo['mime']);
$im=$fun($url);
imageresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun=str_replace('/','',$imgInfo['mime']);
$fun($temp_img);
$imgstr=ob_get_contents();
ob_end_clean();
imagedestroy($im);
return$imgstr;
}
$imgUrl=$_GET['url'];
$info=array();
$string=thumb($imgUrl,500,500,$info);
$mimeArray=explode("/",$info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo$string;
以上代码存为thumb.php,调用效果:
‘玖’ php图片可以等比例的缩放吗
可以。
等比例缩放的方法是:
1、载入选区--自由变换。如下图:
2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。
‘拾’ 如何缩小图片分辨率 本人有若干张图片要传到网上,想把图片分辨率调小一些有什么软件支持批量转换
批量修改图片分辨率的方法:
步骤1,安装工具软件后打开使用,点击软件左边五个功能中的【更改尺寸】功能,然后再点击【添加文件】按钮将需要修改分辨率的图片添加到软件中。