|
php基礎(chǔ)練習(xí)--圖片縮放:
復(fù)制代碼 代碼如下:
<?php
/**
* image zoom.
*/
function imageZoom($filename, $w, $h) {
/* Arguments meaning */
/* $filename: the source of the name */
/* $w: you want get the image's width */
/* $h: you want get the imgage's height */
$arr = getimagesize($filename);
$src_w = $arr[0];
$src_h = $arr[1];
$src_t = $arr[2];
/*1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),
= TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,
= IFF,15 = WBMP,16 = XBM*/
$src_m = $arr['mime'];
$src_img = imagecreatefromjpeg($filename);
if (($w / $src_w) >($h / $src_h)) {
$bili = $h / $src_h;
} else {
$bili = $w / $src_h;
}
$dst_w = $src_w * $bili;
$dst_h = $src_h * $bili;
$dst_img = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
header("content-type:{$src_m}");
switch ($src_t) {
case 1:
$imgout = "imagegif";
break;
case 2:
$imgout = "imagejpeg";
break;
case 3:
$imgout = "imagepng";
break;
default:
echo "The type was wrong!";
break;
}
$dst_filename = "s_".$filename;
$imgout($dst_img, $dst_filename);
imagedestroy($dst_img);
}
$filename = 'gg.jpg';
imageZoom($filename, 100, 200);
核心:<1>注意縮放比例如何得到,雖然這樣得到的圖片可能會與預(yù)想的有點差別,但是最起碼保證了縮放比例。
<2>類型的控制。
php技術(shù):php圖片縮放實現(xiàn)方法,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。