php图片比例缩放
A. 如何实现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);
B. 如何实现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);
}
}
?>
C. 关于php图片缩放问题,比如一张400*300的图片
1、html页面不能对图片有宽度和高度限制;
2、php进行缩放的话,你用的是GD?可以尝试缩放的时候等比缩放:
前面getimagesize,imagesx,imagesy什么的,我省略了,直接获得当前的图片的信息:
$size = array(
's' => array('width'=>$savewidth, 'height'=>$saveheight),
'o' => array('width'=>$width, 'height'=>$height),
);
//s代表当前图片的宽高,
//o代表规则图片的宽高,(就是你的200*200,超出就缩放的规则标准)
function parseimageresizerule($size = array()){
$extract = extract($size);
if($s['width'] >= $o['width'] || $s['height'] >= $o['height'
if($s['width'] >= $o['width']){
$radio['w'] = $o['width'] / $s['width'];
$state['w'] = true;
}
if($s['height'] >= $o['height']){
$radio['h'] = $o['height'] / $s['height'];
$state['h'] = true;
}
if($state['w'] && $state['h']){
if($radio['w'] < $radio['h']){
$radio['s'] = $radio['w'];
$radio['h'] = false;
}else{
$radio['s'] = $radio['h'];
$radio['w'] = false;
}
}elseif($state['w']){
$radio['s'] = $radio['w'];
}else{
$radio['s'] = $radio['h'];
}
$width = intval($s['width'] * $radio['s']);
$height = intval($s['height'] * $radio['s']);
$top = 0;
$left = 0;
}else{
$width = $s['width'];
$height = $s['height'];
$top = intval(($o['height'] - $height) / 2);
$left = intval(($o['width'] - $width) / 2);
}
return array(
'width' => $width,
'height' => $height,
'top' => $top,
'left' => $left,
);
}
最后返回的数组是实际图片的长宽以及200,150这个图片在200*200的图片里的上左距离;
再用
imagefill($filesave, 0, 0, $white);
imageresampled($filesave, $filecache, $left, $top, 0, 0, $width, $height, $savewidth, $saveheight);
就得到1个等比缩放完成的图片!明白?
代码我手敲的,原理肯定可以的~ !
By ahonronline
D. 随便下载一张图片,使用PHP将图片缩放到原来的1/2
//如果是JPG格式,则生成一个同比例的缩小图
$file="xxx.jpg";
$extend_name=strtolower(substr(strrchr($file,"."),1));
if($extend_name=='jpg'){
$image = imagecreatefromjpeg($full_name);//取原图的数据
}
//如果是gif格式,则生成一个同比例的缩小图
if($extend_name=='gif'){
$image = imagecreatefromgif($full_name);//取原图的数据
}
//如果是png格式,则生成一个同比例的缩小图
if($extend_name=='png'){
$image = imagecreatefrompng($full_name);//取原图的数据
}
//echo $full_name.$full_name_small;
$size=GetImageSize($full_name);
$x=$size[0];
$y=$size[1];
//echo $x." _ ".$y;
//假设首页上的图都是250象素左右,如果缩成150则图像失真太严重,则把所有的图都按这个大小进行等比缩放
//计算缩小比例
$rate=1/2;
$small_x=$size[0]*$rate;
$small_y=$size[1]*$rate;
$small_image = imagecreatetruecolor($small_x, $small_y);
imageCopyResampled($small_image,$image,0,0,0,0,$small_x,$small_y,$x,$y);
if(imagejpeg($small_image,$full_name_small)){
ImageDestroy($small_image);
}else{
}
E. 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即为一个缩放后的文件
F. 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);
?>
G. php图片可以等比例的缩放吗
可以。
等比例缩放的方法是:
1、载入选区--自由变换。如下图:
2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。
H. 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语言能够有所帮助。
I. 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模块实现图片缩放、裁剪、压缩示例
J. 求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,调用效果: