重複の可能性:
PHP 再構築イメージ: メモリ使用量
簡単な画像アップロード スクリプトがあります。ユーザーは、最大ファイル サイズが 10 MB の画像 (gif、jpg、または png) をアップロードできます。ユーザーは画像にクロップを適用することもできるため、スクリプトのメモリ制限を 256MB に設定しました。256MB は、10MB の画像をトリミングするのに十分なメモリであると考えました。私は間違っていた。ユーザーが大きな画像 (約 5000x5000) をアップロードし、かろうじてトリミングすると、スクリプトは常にメモリ不足エラーをスローします。PHPで画像のサイズを変更するときのメモリ制限を判断するのに役立つこの便利なツールを見つけました。私もこの式に出くわしました
$width * $height * $channels * 1.7
イメージに必要なメモリ量を決定します。ここで何が起こっているのか説明してくれる人を探しています。10MB の jpeg がメモリにロードされたときに 10MB ではないことは明らかですが、どのくらいのメモリを消費するかをどのように判断できますか? 上の式は正しいですか?大きな画像をトリミングするより効率的な方法はありますか、それとも大量のメモリを使用する必要がありますか?
興味のある方のために、画像をトリミングするコードを次に示します。
function myCropImage(&$src, $x, $y, $width, $height) {
$src_width = imagesx($src);
$src_height = imagesy($src);
$max_dst_width = 1024;
$dst_width = $width;
$dst_height = $height;
$max_dst_height = 768;
// added to restrict size of output image.
// without this check an out of memory error is thrown.
if($dst_width > $max_dst_width || $dst_height > $max_dst_height) {
$scale = min($max_dst_width / $dst_width, $max_dst_height / $dst_height);
$dst_width *= $scale;
$dst_height *= $scale;
}
if($x < 0) {
$width += $x;
$x = 0;
}
if($y < 0) {
$height += $y;
$y = 0;
}
if (($x + $width) > $src_width) {
$width = $src_width - $x;
}
if (($y + $height) > $src_height) {
$height = $src_height - $y;
}
$temp = imagecreatetruecolor($dst_width, $dst_height);
imagesavealpha($temp, true);
imagefill($temp, 0, 0, imagecolorallocatealpha($temp, 0, 0, 0, 127));
imagecopyresized($temp, $src, 0, 0, $x, $y, $dst_width, $dst_height, $width, $height);
return $temp;
}