重複の可能性:
サイズ変更のための画像サイズ比の計算
私が取り組んでいるプロジェクトの画像の独自のカスタム サイズ変更が必要です。不必要なオーバーヘッドを追加したくないので、ライブラリを使用したくありません。クラスに画像の拡大縮小とトリミングを行うことができました。しかし、それは私が望むものを私に与えてくれるわけではありません。現在それ:
- 画像のサイズを変更します
- 特定の高さにトリミングします
しかし、私はそれが必要です:
- 指定されたサイズに収まるように画像のサイズを変更します。したがって、300 x 300 に収まらなければならない 1000 x 500 の画像は、サイズを変更して、最も大きな面が収まるようにする必要があります。つまり、1000 / (3 + 1/3) = 300 なので、長さは 500 / (3 + 1/3) = 150 でなければなりません。
- これで、300 x 150 の画像ができましたが、目的の 300 x 300 スペースに収まりません。したがって、最後のステップは、この 300 x 150 の画像を 300 x 300 の白いキャンバスの中央に配置することです。
以下は私がこれまでに持っているものですが、前述したように、2番目のトリミングステップがありません:
class scale_and_crop
{
public $width;
public $height;
public function __construct($width = 150, $height = 150) {
$this->height = $height;
$this->width = $width;
}
public function process($source, $target) {
// 0. See if already geneated
if (file_exists($target)) {
// do nothing
}
else {
// 1. recursively create folders
$targetFolders = explode('/', $target);
$mkThisDir = './';
$count = 0;
foreach ($targetFolders as $t) {
if ($count == sizeof($targetFolders) - 1 ) continue;
$mkThisDir .= $t.'/';
if (!file_exists($mkThisDir) ) {
mkdir($mkThisDir);
}
$count++;
}
// 2. ... and then copy
$realTarget = './'.$target;
copy( './'.$source, $realTarget);
// 3. Resize
$code = '.jpg';
$image_path = $source;
$thumb_width = $this->width;
$thumb_height = $this->height;
if (!(is_integer($thumb_width) && $thumb_width > 0) && !($thumb_width === "*")) {
echo "The width is invalid";
exit(1);
}
if (!(is_integer($thumb_height) && $thumb_height > 0) && !($thumb_height === "*")) {
echo "The height is invalid";
exit(1);
}
if (!file_exists($image_path)) {
$image_path = strtolower($image_path);
if (!file_exists($image_path)) {
$image_path = strtoupper($image_path);
if (!file_exists($image_path)) {
print 'IMAGE_NOT_AVAILABLE';
die();
}
}
}
$source_image = null;
$extension = pathinfo($image_path, PATHINFO_EXTENSION);
switch ($extension) {
case "jpg":
case "jpeg":
$source_image = imagecreatefromjpeg($image_path);
break;
case "gif":
$source_image = imagecreatefromgif($image_path);
break;
case "png":
$source_image = imagecreatefrompng($image_path);
break;
default:
print 'Fatal IMAGE Error: E838209-837620002'; die();
break;
}
if ($source_image === FALSE) {
die("Fatal error #42870664: could not generate image.");
}
$source_width = imageSX($source_image);
$source_height = imageSY($source_image);
if (($source_width / $source_height) == ($thumb_width / $thumb_height)) {
$source_x = 0;
$source_y = 0;
}
if (($source_width / $source_height) > ($thumb_width / $thumb_height)) {
$source_y = 0;
$temp_width = $source_height * $thumb_width / $thumb_height;
$source_x = ($source_width - $temp_width) / 2;
$source_width = $temp_width;
}
if (($source_width / $source_height) < ($thumb_width / $thumb_height)) {
$source_x = 0;
$temp_height = $source_width * $thumb_height / $thumb_width;
$source_y = ($source_height - $temp_height) / 2;
$source_height = $temp_height;
}
$target_image = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresampled($target_image, $source_image, 0, 0, $source_x, $source_y, $thumb_width, $thumb_height, $source_width, $source_height);
$newFile = $target;
$returnUrl = '';
switch ($extension) {
case "jpg":
case "jpeg":
imagejpeg($target_image, $newFile, 100);
$returnUrl = $newFile;
break;
case "gif":
imagegif($target_image, $newFile);
$returnUrl = $newFile;
break;
case "png":
imagepng($target_image, $newFile, 9);
$returnUrl = $newFile;
break;
default:
exit(1);
break;
}
imagedestroy($target_image);
imagedestroy($source_image);
}
}
}
思い通りにトリミングするにはどうすればよいですか?