• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何实现php代码处理图片

php 搞代码 3年前 (2022-01-22) 28次浏览 已收录 0个评论

本篇文章主要介绍如何实现php代码处理图片,感兴趣的朋友参考下,希望对大家有所帮助。

一、 图片缩放代码

非等比例图片缩放代码

$fileNames='./images/hee.jpg';list($s_w,$s_h)=getimagesize($fileNames);$per=0.3;$n_w=$s_w*$per;$n_h=$s_h*$per;$s_img=imagecreatefromjpeg($fileNames);$n_img=imagecreatetruecolor($n_w,$n_h);imagecopyresized($n_img,$s_img,0,0,0,0,$n_w,$n_h,$s_w,$s_h);header("Content-Type:image/jpeg");imagejpeg($n_img,'./images/hee2.jpg');imagedestroy($n_img);imagedestroy($s_img);function sf($backgroung,$bl,$location){list($width,$height)=getimagesize($backgroung);$n_w=$width*$bl;$n_h=$height*$bl;$s_img=imagecreatefromjpeg($backgroung);$n_img=imagecreatetruecolor($n_w,$n_h);imagecopyresized($n_img,$s_img,0,0,0,0,$n_w,$n_h,$width,$height);header("Content-Type:image/jpeg");imagejpeg($n_img,$location);imagedestroy($s_img);imagedestroy($n_img);}//sf('./images/hee.jpg',0.3,'./images/hee9.jpg');

等比例图片缩放(并且加了透明色处理)

function imagesf($backgroung,$n_w,$n_h,$newFiles){list($s_w,$s_h,$imagetype)=getimagesize($backgroung);//等比例缩放固定算法if ($n_w && ($s_w < $s_h)) {$n_w = ($n_h / $s_h) * $s_w;} else {$n_h = ($n_w / $s_w) * $s_h;}if($imagetype==2){$s_img=imagecreatefromjpeg($backgroung);}elseif($imagetype==3){$s_img=imagecreatefrompng($backgroung);}elseif($imagetype==1){$s_img=imagecreatefromgif($backgroung);}else{echo "php 不支持这中图片类型!!";}$n_img=imagecreatetruecolor($n_w,$n_h);//取得原图形的索引$img_index=imagecolortransparent($s_img);//判断原图形索引是否在图形调色板的数目范围内(调色板数目 0~255);if($img_index>=0 && $img_index<imagecolorstotal($s_img)){//返回一个具有 red,green,blue 和 alpha 的键名的关联数组,包含了指定颜色索引的相应的值$trans=imagecolorsforindex($s_img,$img_index);//print_r($trans); Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 )//取原图的颜色,给新图分配颜色$n_color=imagecolorallocate($n_img,$trans['red'],$trans['green'],$trans['blue']);imagefill($n_img,0,0,$n_color);//把获取到的颜色,设置为新图的透明色imagecolortransparent($n_img,$n_color);}//imagecopyresampled($n_img,$s_img,0,0,0,0,$n_w,$n_h,$s_w,$s_h);if(imagetypes()& IMG_JPG){header("Content-Type:image/jpeg");imagejpeg($n_img,$newFiles);}elseif(imagetypes()& IMG_PNG){header("Content-Type:image/png");imagepng($n_img,$newFiles);}elseif(imagetypes()& IMG_GIF){header("Content-Type:image/gif");imagegif($n_img,$newFiles);}else{echo "php 不支持这中图片类型!!";}imagedestroy($s_img);imagedestroy($n_img);}imagesf('./images/map.gif',200,200,'./images/map2.gif');

二、 图片裁剪代码

图片裁剪

function imagecut($backgroung,$cut_x,$cut_y,$cut_width,$cut_hight,$fileNames){list(,,$imagetype)=getimagesize($backgroung);if($imagetype==1){$s_img=imagecreatefromgif($backgroung);}elseif($imagetype==2){$s_img=imagecreatefromjpeg($backgroung);}elseif($imagetype==2){$s_img=imagecreatefrompng($backgroung);}else{echo "php 不支持这种图片类型!!";}$n_img=imagecreatetruecolor($cut_width,$cut_hight);imagecopyresized($n_img,$s_img,0,0,$cut_x,$cut_y,$cut_width,$cut_hight,$cut_width,$cut_hight);if(imagetypes() & IMG_GIF){header("Content-Type:image/gif");imagegif($n_img,$fileNames);}elseif(imagetypes() & IMG_JPG){header("Content-Type:image/jpeg");imagejpeg($n_img,$fileNames);}elseif(imagetypes() & IMG_PNG){header("Content-Type:image/png");imagepng($n_img,$fileNames);}else{echo "php 不支持这种图片类型!!";}}//imagecut('./images/hee.jpg',52, 47, 345, 330,'./images/hee4.jpg');function cut($backgroung,$x,$y,$cut_width,$cut_hight,$location){$s_img=imagecreatefromjpeg($backgroung);$n_img=imagecreatetr<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码!网!</b><strong>搞gaodaima代码</strong>uecolor($cut_width,$cut_hight);imagecopy($n_img,$s_img,0,0,$x,$y,$cut_width,$cut_hight);header("Content-Type:image/jpeg");imagejpeg($n_img,$location);imagedestroy($s_img);imagedestroy($n_img);}//cut('./images/hee.jpg',52, 47, 345, 330,'./images/hee8.jpg');


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何实现php代码处理图片

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址