php按照指定的尺寸压缩图片
/** * * 按照指定的尺寸压缩图片 * * @param $source_path 原图路径 * * @param $imgWidth 目标宽度 * * @param $imgHeight 目标高度 * * @return bool|string */ function resize_image($source_path, $imgWidth, $imgHeight) { $source_info = getimagesize($source_path); $source_mime = $source_info['mime']; switch ($source_mime) { case 'image/gif': $source_image = imagecreatefromgif($source_path); break; case 'image/jpeg': $source_image = imagecreatefromjpeg($source_path); break; case 'image/png': $source_image = imagecreatefrompng($source_path); break; default: return false; break; } $target_image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图 imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]); if(!imagejpeg($target_image, $source_path)){ $source_path = ''; } imagedestroy($target_image); return $source_path; }
本文作者:潘潘潘的博客
本文链接:https://www.cnblogs.com/seanpan/p/17373694.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步