PHPで画像のサイズを変更し、アスペクト比、ファイルがagif
またはpng
画像の場合は透明度を維持し、最終的なサイズに応じて画像を垂直方向または水平方向に中央揃えするにはどうすればよいですか?
3647 次
6 に答える
2
いくつかのトリックがあります。ここに私の機能があります:
function im_resize($file_src,$file_dest,$wd,$hd) {
if (!file_exists($file_src)) return false;
$size = getimagesize($file_src);
if ($size === false) return false;
if ($size['mime']=='image/pjpeg') $size['mime'] = 'image/jpeg';
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
$destformat = strtolower(substr($file_dest, -4));
$icfunc = "imagecreatefrom" . $format;
if (!function_exists($icfunc)) return false;
$src = $icfunc($file_src);
$ws = imagesx($src);
$hs = imagesy($src);
if ($ws >= $hs) {
$hd = ceil(($wd * $hs) / $ws);
}
else {
$wd = ceil(($hd*$ws)/$hs);
}
if ($ws <= $wd) {
$wd = $ws;
$hd = $hs;
}
$wc=($wd * $hs) / $hd;
if ($wc<=$ws) {
$hc=($wc * $hd) / $wd;
}
else {
$hc=($ws * $hd) / $wd;
$wc=($wd * $hc) / $hd;
}
$dest = imagecreatetruecolor($wd,$hd);
switch ($format) {
case "png":
imagealphablending( $dest, false );
imagesavealpha( $dest, true );
$transparent = imagecolorallocatealpha($dest, 255, 255, 255, 127);
imagefilledrectangle($dest, 0, 0, $nw, $nh, $transparent);
break;
case "gif":
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($src, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($src, $background);
break;
}
imagecopyresampled($dest,$src,0,0,($ws-$wc)/2,($hs-$hc)/2, $wd, $hd, $wc, $hc);
if (!isset($q)) $q = 100;
if ($destformat=='.png') $saved=imagepng($dest,$file_dest);
if ($destformat=='.jpg') $saved=imagejpeg($dest,$file_dest,$q);
if (!$saved) my_error_log('saving failed');
imagedestroy($dest);
imagedestroy($src);
@chmod($file_dest, 0666);
return true;
}
于 2010-11-09T12:53:49.570 に答える
1
もしも
(…) 次に、画像を垂直方向または水平方向の中央に配置します (…)
周りに白または黒の境界線を追加したいという意味ですが、私はそうしません。サイズ変更された画像を保存し、html/css を使用してブラウザーでセンタリングを行うだけです。
于 2010-11-09T12:53:51.473 に答える
0
// ---------------------------------------------------------------------------------
// Resize Image
// ---------------------------------------------------------------------------------
function ResizeImage($FileName,$SaveFile, $MaxWidth, $MaxHeight = null) {
$extension = GetFileExtension($FileName);
switch(strtolower($extension)) {
case "gif":
$objImage = imagecreatefromgif($FileName);
break;
case "png":
$objImage = imagecreatefrompng($FileName);
break;
default:
$objImage = imagecreatefromjpeg($FileName);
break;
}
list($width, $height, $type, $attr) = getimagesize($FileName);
$TargetWidth = $width;
$TargetHeight = $height;
if (!is_null($MaxWidth)) {
if ($MaxWidth < $TargetWidth) {
$TargetWidth = $MaxWidth;
$TargetHeight = round($TargetHeight * $TargetWidth / $width);
}
}
if (!is_null($MaxHeight)) {
if ($MaxHeight < $TargetHeight) {
$TargetHeight = $MaxHeight;
$TargetWidth = round($TargetWidth * $TargetHeight / $height);
}
}
$DestImage = imagecreatetruecolor($TargetWidth, $TargetHeight);
// handle transparancy
if ( ($type == IMAGETYPE_GIF) || ($type == IMAGETYPE_PNG) ) {
$trnprt_indx = imagecolortransparent($objImage);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($objImage, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($DestImage, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($DestImage, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($DestImage, $trnprt_indx);
} elseif ($type == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($DestImage, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($DestImage, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($DestImage, 0, 0, $color);
// Restore transparency blending
imagesavealpha($DestImage, true);
}
}
imagecopyresampled($DestImage, $objImage, 0, 0, 0, 0, $TargetWidth, $TargetHeight, $width, $height);
switch(strtolower($extension)) {
case "gif":
imagegif($DestImage, $SaveFile);
break;
case "png":
imagepng($DestImage, $SaveFile,0);
break;
default:
imagejpeg($DestImage,$SaveFile,100);
break;
}
}
// ---------------------------------------------------------------------------------
// GetFileExtension
// ---------------------------------------------------------------------------------
function GetFileExtension($inFileName) {
return substr($inFileName, strrpos($inFileName, '.') + 1);
}
于 2010-11-09T12:56:02.100 に答える
0
まず、getimagesize(); を使用して、サイズを変更したい画像の画像サイズを読み込みます。
サイズ変更した画像の最大幅と高さを決定します。
サイズ変更する画像の幅または高さのうち、どちらが大きいかを調べます。
サイズ変更の幅または高さに収まるようにするために、その辺をどれだけ分割する必要があるかを調べます。その値を取得し、サイズ変更する画像の反対側を分割します。
これで、サイズ変更された画像の幅と高さが得られました。imagecopyresampled() を使用するだけです。
于 2010-11-09T12:56:41.593 に答える
0
過去に使用した Image_Transform (http://pear.php.net/package/Image_Transform) という PEAR パッケージがあります。画像を操作するための優れた機能がたくさんあり、非常に使いやすいです (高度な PHP プログラマーである必要はありません)。
于 2010-11-09T12:57:06.480 に答える