私は現在、ユーザーがイメージをアップロードできる必要があるPHP Webサイトを作成しています。それで、適切なサイズにリサイズする必要があります。私のPHP設定は50MBのメモリ制限に設定されています。イメージのサイズ変更のPHPメモリ使用量を減らす
問題は約5MBを超えて画像をアップロードすると、(メモリの使用量が多いため)画像のサイズを変更できません。だから私は、とにかくサイズを変更する画像のメモリ使用量を最適化するかどうか疑問に思っていた。ここでは、私が現在使用しているものです:
class Image {
var $uploaddir;
var $quality = 80;
var $ext;
var $dst_r;
var $img_r;
var $img_w;
var $img_h;
var $output;
var $data;
var $datathumb;
function setFile($src = null) {
$this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
$this->img_r = ImageCreateFromJPEG($src);
} elseif(is_file($src) && $this->ext == "PNG") {
$this->img_r = ImageCreateFromPNG($src);
} elseif(is_file($src) && $this->ext == "GIF") {
$this->img_r = ImageCreateFromGIF($src);
}
$this->img_w = imagesx($this->img_r);
$this->img_h = imagesy($this->img_r);
}
function resize($largestSide = 100) {
$width = imagesx($this->img_r);
$height = imagesy($this->img_r);
$newWidth = 0;
$newHeight = 0;
if($width > $height){
$newWidth = $largestSide;
$newHeight = $height * ($newWidth/$width);
}else{
$newHeight = $largestSide;
$newWidth = $width * ($newHeight/$height);
}
$this->dst_r = ImageCreateTrueColor($newWidth, $newHeight);
imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$this->img_r = $this->dst_r;
$this->img_h = $newHeight;
$this->img_w = $newWidth;
}
function createFile($output_filename = null) {
if($this->ext == "JPG" OR $this->ext == "JPEG" OR $this->ext == "PNG" OR $this->ext == "GIF") {
imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'."jpg", $this->quality);
} /*elseif($this->ext == "PNG") {
imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
} elseif($this->ext == "GIF") {
imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
}*/
$this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
}
function setUploadDir($dirname) {
$this->uploaddir = $dirname;
}
function flush() {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
imagedestroy($this->dst_r);
unlink($targetFile);
imagedestroy($this->img_r);
}
そして、私が行うリサイズのために:
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file ($tempFile, $targetFile);
$image = new Image();
$image->setFile($targetFile);
$image->setUploadDir($targetPath);
$image->resize(200);
$image->createFile(md5($id));
$image->flush();
私はuploadify使用して、現在のだが、それは、このアップロード/スクリプトのサイズを変更し使用しています。このコードを最適化してメモリを少なくする方法はありますか?
ありがとうございました:)
ありがとう、私は実際のファイルサイズではなく、ファイルの解像度が問題であると考えました。あなたが提供したリンクを使用して検証を行っています。どうもありがとう。また、私は2848 x 4288(12mp)で画像をアップロードできないため、.htaccessを使用してメモリー制限を増やしました。これはカメラで画像には普通のことだと思います。 – AzaraT