设为首页 收藏本站
查看: 692|回复: 0

[经验分享] 使用PHP缩略图和剪切图

[复制链接]

尚未签到

发表于 2017-4-1 11:41:57 | 显示全部楼层 |阅读模式
API:
resource imagecreatetruecolor ( int $width , int $height )
magecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
是否定义了本函数取决于 PHP 和 GD 的版本。从 PHP 4.0.6 到 4.1.x 只要加载了 GD 模块本函数一直存在,但是在没有安装 GD2 的时候调用,PHP 将发出致命错误并退出。在 PHP 4.2.x 中此行为改为发出警告而不是错误。其它版本只在安装了正确的 GD 版本时定义了本函数。
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
$dst_image:新建的图片
$src_image:需要载入的图片
$dst_x:设定需要载入的图片在新图中的x坐标
$dst_y:设定需要载入的图片在新图中的y坐标
$src_x:设定载入图片要载入的区域x坐标
$src_y:设定载入图片要载入的区域y坐标
$dst_w:设定载入的原图的宽度(在此设置缩放)
$dst_h:设定载入的原图的高度(在此设置缩放)
$src_w:原图要载入的宽度
$src_h:原图要载入的高度
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
imagejpeg() 从 image 图像以 filename 为文件名创建一个 JPEG 图像。
ool imagedestroy ( resource $image )
imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。

http://www.cnblogs.com/xiaomia/archive/2010/11/13/1876191.html
一开始采用了 imagecopyresized 方法进行图像等比缩小,实际操作后发现,图像缩小后燥点非常严重。后再换用 imagecopysampled 方法,该方法会对图像进行重新采样,对缩小的图像进行平滑处理,使清晰度得到很大提高
list($src_w,$src_h)=getimagesize($src_img);  // 获取原图尺寸
$dst_scale = $dst_h/$dst_w; //目标图像长宽比
$src_scale = $src_h/$src_w; // 原图长宽比
if($src_scale>=$dst_scale){  // 过高
$w = intval($src_w);
$h = intval($dst_scale*$w);
$x = 0;
$y = ($src_h - $h)/3;
}
else{ // 过宽
$h = intval($src_h);
$w = intval($h/$dst_scale);
$x = ($src_w - $w)/2;
$y = 0;
}
// 剪裁
$source=imagecreatefromjpeg($src_img);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h);
// 缩放
$scale = $dst_w/$w;
$target = imagecreatetruecolor($dst_w, $dst_h);
$final_w = intval($w*$scale);
$final_h = intval($h*$scale);
imagecopyresampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h);
// 保存
$timestamp = time();
imagejpeg($target, "$timestamp.jpg");
imagedestroy($target);

imagecopyresampled
public static function short($src_from, $destFile, $scale = 0.5)
{
$image=imagecreatefromjpeg($src_from);
list($src_w, $src_h) = getimagesize($src_from);
$new_w = intval($src_w * $scale);
$new_h = intval($src_h * $scale);
$target = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($target, $image, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
imagejpeg($target, $destFile);
imagedestroy($target);
return $destFile;
}

http://www.cnblogs.com/analyzer/articles/1267017.html
先说说缩略图,它用得比较多,代码如下:
<?php   
header("Content-type: image/png");   
//原图   
$filename='source.jpg';   
//缩放比例 新图/原图   
$percent = '0.5';   
list($width,$height) = getimagesize($filename);   
$newwidth = $width * $percent;   
$newheight = $height * $percent;   
// Load   
$thumb = imagecreatetruecolor($newwidth, $newheight);   
$source = imagecreatefromjpeg($filename);   
// Resize   
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);   
// Output   
imagepng($thumb);   
?>
我自己ckaephp的例子:
public static function doThumbnail($filePath,$newWidth=273, $newHeight=156){
$fileType=CommonUtils::getFileType($filePath);
$lastIndexOf = strrpos($filePath,".");
$target=substr($filePath,0,$lastIndexOf)."_thumbnail".substr($filePath,$lastIndexOf);
$arr = getimagesize($filePath);
if(strcasecmp("png",$fileType)===0){
$iOut = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($iOut, false);
imagesavealpha($iOut, true);
$source = imagecreatefrompng($filePath);
imagealphablending($source, true);
imagecopyresampled($iOut, $source, 0, 0, 0, 0, $newWidth, $newHeight, $arr[0],$arr[1]);
imagepng($iOut,$target);
}else{
$image=imagecreatefromjpeg($filePath);
$iOut = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($iOut,$image,0,0,0,0,$newWidth, $newHeight,$arr[0],$arr[1]);
imagejpeg($iOut, $target);
imagedestroy($iOut);
}
return $target;
}



再说说剪切图,就是不缩放,而是从原图中剪切出一块小图,比较个性。代码如下:
<?php   
$maxW=300;   
$maxH=300;   
//图片路径   
$link= "big.jpg";   
$img = imagecreatefromjpeg($link);   
list($width, $height, $type, $attr) = getimagesize($link);   
$widthnum=ceil($width/$maxW);   
$heightnum=ceil($height/$maxH);   
$iOut = imagecreatetruecolor ($maxW,$maxH);   
//bool imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )   
//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。   
//整图循环切割   
for ($i=0;$i < $heightnum;$i++) {   
for ($j=0;$j < $widthnum;$j++) {   
imagecopy($iOut,$img,0,0,($j*$maxW),($i*$maxH),$maxW,$maxH);//复制图片的一部分   
imagejpeg($iOut,"images/".$i."_".$j.".jpg"); //输出成0_0.jpg,0_1.jpg这样的格式   
}   
}   
//只剪切一个开始部位的小图.复制图片的一部分   
imagecopy($iOut,$img,0,0,0,0,$maxW,$maxH);   
imagejpeg($iOut,"images/sm.jpg");   
?>
我的例子:
//图片路径
$link = "/mnt/D/work_documents/htdocs/storage_file/2014_11_02/p195ncv6ub1h0f1icf11hpahc16rq8.jpg";
$img = imagecreatefromjpeg($link);
list($width, $height, $type, $attr) = getimagesize($link);
$iOut = imagecreatetruecolor(intval($this->request->data['width']), intval($this->request->data['height']));//给宽度和高度
/*
* bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
*/
imagecopy($iOut, $img, 0, 0, $this->request->data['x1'], intval($this->request->data['y1']), intval($this->request->data['width']), intval($this->request->data['height'])); //复制图片的一部分
imagejpeg($iOut, "/mnt/D/work_documents/htdocs/storage_file/2014_11_02/bak/aa.jpg"); //输出成0_0.jpg,0_1.jpg这样的格式

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-358663-1-1.html 上篇帖子: PHP实现折半(二分)查找算法 下篇帖子: Using node.js to store PHP sessions
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表