本文目錄一覽:
php圖片可以等比例的縮放嗎
可以。
等比例縮放的方法是:
1、載入選區–自由變換。如下圖:
2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。
求php圖片縮放處理函數
?php
/**
* 圖片縮放
* @param string $url
* @param int $maxWidth
* @param int $maxHeight
* @return string
*/
function thumb($url, $maxWidth, $maxHeight, $info) {
$info = $imgInfo = getimagesize($url);
$width = $imgInfo[0];//獲取圖片寬度
$height = $imgInfo[1];//獲取圖片高度
$r = min($maxHeight/$height, $maxWidth/$width);
if($r = 1) { // 不用縮放
$maxHeight = $height;
$maxWidth = $width;
} elseif($r 1) { // 縮放
$maxHeight = $height * $r;
$maxWidth = $width * $r;
}
$temp_img = imagecreatetruecolor($maxWidth,$maxHeight); //創建畫布
$fun = str_replace(‘/’, ‘createfrom’, $imgInfo[‘mime’]);
$im = $fun($url);
imagecopyresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun = str_replace(‘/’, ”, $imgInfo[‘mime’]);
$fun($temp_img);
$imgstr = ob_get_contents();
ob_end_clean();
imagedestroy($im);
return $imgstr;
}
$imgUrl = $_GET[‘url’];
$info = array();
$string = thumb($imgUrl, 500, 500, $info);
$mimeArray = explode(“/”, $info[‘mime’]);
header(“Content-Type:image/{$mimeArray[1]}”);
echo $string;
以上代碼存為thumb.php,調用效果:
php實現圖片等比例縮放代碼
新建文件index.php,需要在統計目錄下有個圖片為q.jpg(可根據源碼進行更改圖片的名稱)
源代碼如下:
?php
$filename=”q.jpg”;
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷貝部分圖像並調整
imagecopyresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//圖像輸出新圖片、另存為
imagejpeg($new,
“q1.jpg”);
imagedestroy($new);
imagedestroy($img);
?
使用瀏覽器運行過後,在index.php同級的目錄下會有個q1.jpg,這個圖片就是等比例縮放後的圖片,路徑可以自己在源代碼裡面更改,放在自己的項目當中去或寫個方法也行
以上所述上就是本文的全部內容了,希望對大家學習php語言能夠有所幫助。
原創文章,作者:UFJQ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/140276.html