PHP 对 png 图像进行缩放,支持透明背景

PHP 对 png 图像进行缩放,支持透明背景

function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = ‘file’, $delete_original = true, $use_linux_commands = false )
{
if ( $height <= 0 && $width <= 0 ) {
return false;
}
$info = getimagesize($file);
$image = ”;

$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;

if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min ( $width / $width_old, $height / $height_old);
$final_width = round ($width_old * $factor);
$final_height = round ($height_old * $factor);

}
else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
}

switch ($info[2] ) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
break;
default:
return false;
}

$image_resized = imagecreatetruecolor( $final_width, $final_height );

if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$trnprt_indx = imagecolortransparent($image);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image’s transparent color’s RGB values
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image_resized, $trnprt_color[‘red’], $trnprt_color[‘green’], $trnprt_color[‘blue’]);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($image_resized, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don’t have one allocated already
elseif ($info[2] == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($image_resized, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);

// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $color);

// Restore transparency blending
imagesavealpha($image_resized, true);
}
}

imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);

if ( $delete_original ) {
if ( $use_linux_commands )
exec(‘rm ‘.$file);
else
@unlink($file);
}

switch ( strtolower($output) ) {
case ‘browser’:
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case ‘file’:
$output = $file;
break;
case ‘return’:
return $image_resized;
break;
default:
break;
}

switch ($info[2] ) {
case IMAGETYPE_GIF:
imagegif($image_resized, $output);
break;
case IMAGETYPE_JPEG:
imagejpeg($image_resized, $output);
break;
case IMAGETYPE_PNG:
imagepng($image_resized, $output);
break;
default:
return false;
}

return true;
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » PHP 对 png 图像进行缩放,支持透明背景

134 评论

  1. order avodart generic zofran usa zofran 8mg uk

  2. cheap isotretinoin 10mg amoxil generic buy generic zithromax 250mg

  3. order cialis 5mg for sale us cialis sales buy sildenafil 50mg for sale

  4. order altace 10mg without prescription temovate tablet azelastine 10ml brand

  5. hytrin 1mg pill cheap arava 10mg purchase sulfasalazine online

  6. order alendronate 70mg online cheap paracetamol price cost famotidine 20mg

  7. order olmesartan 20mg for sale buy verapamil acetazolamide order

  8. cialis without a doctor prescription tadalafil 5mg cheap buy anafranil generic

  9. dapoxetine online order cytotec brand buy synthroid 150mcg generic

  10. brand ozobax tizanidine price buy ketorolac without prescription

  11. cialis 40mg cost oral sildenafil sildenafil fast shipping

  12. zithromax pills neurontin pill order gabapentin generic

  13. retin gel sale cheap avanafil avanafil 200mg over the counter

  14. buy tiotropium bromide 9 mcg online cheap minocin online order terazosin 5mg generic

  15. real money slots free spins no deposit ocean casino online live online blackjack

  16. buy benicar 10mg without prescription buy calan sale buy divalproex generic

  17. order generic nexium biaxin tablet order lasix 40mg online

  18. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Onwin , Onwin Giriş , Onwin Güncel Giriş , Onwin Yeni Adres , onwin

  19. purchase femara generic viagra 25 mg purchase viagra pills

  20. order absorica without prescription accutane 40mg oral zithromax online order

  21. Thank you great post. Hello Administ .Click on the Keyword to Enter the Website. onwin

  22. losartan order losartan price purchase topiramate sale

  23. buy tamsulosin 0.4mg pill flomax pills spironolactone pills

  24. After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader. Hacklink Panel Hacklink

  25. carbamazepine 400mg cost tegretol 200mg drug where to buy lincocin without a prescription

  26. order sildenafil 50mg pills suhagra online buy sildenafil 100mg pills for sale

  27. lasix 100mg pills vibra-tabs ca ventolin 2mg oral

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡