PHPで画像のサイズを変更するコード

6533 ワード

まず、自分で書いた関数を紹介します.
 
  
$imgsrc = "http://www.nowamagic.net/images/3.jpg";
$width = 780;
$height = 420;
resizejpg($imgsrc,$imgdst,$width,$height);
function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
{
//$imgsrc jpg $imgdst jpg $imgwidth $imgheight
// ,
$arr = getimagesize($imgsrc);
header("Content-type: image/jpg");
$imgWidth = $imgwidth;
$imgHeight = $imgheight;
// Create image and define colors
$imgsrc = imagecreatefromjpeg($imgsrc);
$image = imagecreatetruecolor($imgWidth, $imgHeight); //
imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
imagepng($image);
imagedestroy($image);
}
?>

imagecopyresampled
imagecopyresampled--一部の画像をコピーしてサイズを変更します.
int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
imagecopyresampled()は、1枚の画像の正方形領域を別の画像にコピーし、画素値を滑らかに挿入するため、特に画像のサイズを小さくしても大きな明瞭度を維持する.dst_imとsrc_imは、それぞれターゲット画像とソース画像の識別子である.ソースとターゲットの幅と高さが異なる場合は、対応する画像の収縮と伸長が行われます.座標は左上を指します.この関数は、dst_imとsrc_imが同じ場合、同じ図の内部で領域をコピーするために使用できますが、領域が重複している場合、結果は予知できません.
注意:パレット画像の制限(255+1色)に問題があります.再サンプリングまたはフィルタリングには通常255色以上が必要です.再サンプリングされた新しいピクセルとその色を計算する際に近似値が使用されます.パレット画像に新しい色を割り当てる場合は、失敗した場合に計算結果が最も近いものを選択します(理論的に)の色.これは常に視覚的に最も近い色ではありません.これは、空白(または視覚的に空白)の画像などの奇妙な結果を生じる可能性があります.この問題をスキップするには、imagecreatetruecolor()で作成されたように、ターゲット画像として真彩色の画像を使用します.
注:imagecopyresampled()にはGD 2.0が必要です.l以降.
簡単な例:
 
  
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>

例2:
 
  
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>

画像のサイズを変更する方法は2つあります.
ImageCopyResized()関数はすべてのGDバージョンで有効ですが、画像をスケーリングするアルゴリズムは粗いです.
ImageCopyResamples()は、ピクセル補間アルゴリズムによって得られる画像エッジが比較的滑らかである.(ただし、この関数の速度はImageCopyResized()より遅い).
2つの関数のパラメータは同じです.次のようになります.
 
  
imageCopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);
imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

例:
 
  
$src = ImageCreateFromJPEG('php.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $widht/2;
$y = $height/2;
$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height);
header('Content-Type : image/png');
ImagePNG($det);
?>

phpでjpg画像ファイルのサイズを変更する方法
 
  

function resize_jpg($img,$w,$h){
// $thumb = imagecreate ($w, $h);
$image = imagecreatefromjpeg($img);
$imagedata = getimagesize($img);
if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);// !
$thumb = imagecreatetruecolor ($w, $h);
imagecopyresized ($thumb, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1]);
imagejpeg($thumb);
}
//resize_jpg($file,$w,$h);
resize_jpg("images/dsc01244.jpg",100,100);
imagedestory($thumb);
imagedestory($image);
?>

関数コード:
 
  
/*
*
* $srcfile ,
* $rate , ,
* $filename jpg
* : resizeimage("zt32.gif",0.1);
* : resizeimage("zt32.gif",250 );
* : HTML IMG SRC
*/
function resizeimage($srcfile,$rate=.5, $filename = "" ){
$size=getimagesize($srcfile);
switch($size[2]){
case 1:
$img=imagecreatefromgif($srcfile);
break;
case 2:
$img=imagecreatefromjpeg($srcfile);
break;
case 3:
$img=imagecreatefrompng($srcfile);
break;
default:
exit;
}
//
$srcw=imagesx($img);
$srch=imagesy($img);
//
if($size[0] <= $rate || $size[1] <= $rate){
$dstw=$srcw;
$dsth=$srch;
}else{
if($rate <= 1){
$dstw=floor($srcw*$rate);
$dsth=floor($srch*$rate);
}else {
$dstw=$rate;
$rate = $rate/$srcw;
$dsth=floor($srch*$rate);
}
}
//echo "$dstw,$dsth,$srcw,$srch ";
//
$im=imagecreatetruecolor($dstw,$dsth);
$black=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,$dstw,$dsth,$black);
imagecopyresized($im,$img,0,0,0,0,$dstw,$dsth,$srcw,$srch);
// JPEG
if( $filename ) {
//
imagejpeg($im, $filename );
}else {
//
imagejpeg($im);
}
//
imagedestroy($im);
imagedestroy($img);
}
?>