/**
* 改變圖片的寬高
*
* @author flynetcn (2009-12-16)
*
* @param string $img_src 原圖片的存放地址或url
* @param string $new_img_path 新圖片的存放地址
* @param int $new_width 新圖片的寬度
* @param int $new_height 新圖片的高度
* @return bool 成功true, 失敗false
*/
function resize_image($img_src, $new_img_path, $new_width, $new_height)
{
$img_info = @getimagesize($img_src);
if (!$img_info || $new_width < 1 || $new_height < 1 || empty($new_img_path)) {
return false;
}
if (strpos($img_info['mime'], 'jpeg') !== false) {
$pic_obj = imagecreatefromjpeg($img_src);
} else if (strpos($img_info['mime'], 'gif') !== false) {
$pic_obj = imagecreatefromgif($img_src);
} else if (strpos($img_info['mime'], 'png') !== false) {
$pic_obj = imagecreatefrompng($img_src);
} else {
return false;
}
$pic_width = imagesx($pic_obj);
$pic_height = imagesy($pic_obj);
if (function_exists("imagecopyresampled")) {
$new_img = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
} else {
$new_img = imagecreate($new_width, $new_height);
imagecopyresized($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
}
if (preg_match('~.([^.]+)$~', $new_img_path, $match)) {
$new_type = strtolower($match[1]);
switch ($new_type) {
case 'jpg':
imagejpeg($new_img, $new_img_path);
break;
case 'gif':
imagegif($new_img, $new_img_path);
break;
case 'png':
imagepng($new_img, $new_img_path);
break;
default:
imagejpeg($new_img, $new_img_path);
}
} else {
imagejpeg($new_img, $new_img_path);
}
imagedestroy($pic_obj);
imagedestroy($new_img);
return true;
}
//test
$ret = resize_image('http://static.php.net/www.php.net/images/php_snow_2008.gif', 'test.png', '300', '400');
var_dump($ret); die;
用php調整圖片寬高
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
VS Code配置PHP XDebug
Lansonli
2020-07-05 09:50:15
php單元測試斷言方法
東子_dz
2020-04-09 22:29:58
二維數組排序
Superman_xing
2020-02-21 19:26:31
Varnish高性能開源HTTP加速器:Varnish Nginx和Varnish Apache搭建配置
lifushan123
2020-02-20 16:45:09
嘗試用Gearman實現分佈式處理(PHP)
lifushan123
2020-02-20 16:45:09
ueditor插件富文本的賦值問題
zhumengstyle
2019-08-07 23:30:46
vscode 自定義代碼片段
云溪111
2019-03-12 15:17:06
如何安裝 Composer
Superman_xing
2018-11-20 08:39:54
刪除目錄下指定類型或格式的文件(多層級刪除)
zhumengstyle
2018-11-14 06:33:27
解決 PHPExcel 長數字串顯示爲科學計數
Superman_xing
2018-10-27 08:32:57
永久使用 Zend Studio
weixin_37329367
2018-09-06 01:40:00
簡單的php中文轉拼音的實現代碼
東子_dz
2018-09-05 03:52:14
PHP mongoDB GridFS 圖片存儲系統
lifushan123
2018-09-04 04:39:31