Note that there are some explanatory texts on larger screens.

plurals
  1. POreducing php memory usage for image resizing
    primarykey
    data
    text
    <p>I'm currently creating a PHP website where users need to be able to upload images, which then need to resized down to a reasonable size. My php configuration is set to a memory limit of 50mb. </p> <p>The problem is when uploading images over around 5mb, php is not able to resize the image(due to to much memory use). So I was wondering if there's anyway to optimize memory usage of a resizing images. Here's what I currently use:</p> <pre><code>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-&gt;ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION)); if(is_file($src) &amp;&amp; ($this-&gt;ext == "JPG" OR $this-&gt;ext == "JPEG")) { $this-&gt;img_r = ImageCreateFromJPEG($src); } elseif(is_file($src) &amp;&amp; $this-&gt;ext == "PNG") { $this-&gt;img_r = ImageCreateFromPNG($src); } elseif(is_file($src) &amp;&amp; $this-&gt;ext == "GIF") { $this-&gt;img_r = ImageCreateFromGIF($src); } $this-&gt;img_w = imagesx($this-&gt;img_r); $this-&gt;img_h = imagesy($this-&gt;img_r); } function resize($largestSide = 100) { $width = imagesx($this-&gt;img_r); $height = imagesy($this-&gt;img_r); $newWidth = 0; $newHeight = 0; if($width &gt; $height){ $newWidth = $largestSide; $newHeight = $height * ($newWidth / $width); }else{ $newHeight = $largestSide; $newWidth = $width * ($newHeight / $height); } $this-&gt;dst_r = ImageCreateTrueColor($newWidth, $newHeight); imagecopyresampled($this-&gt;dst_r, $this-&gt;img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $this-&gt;img_r = $this-&gt;dst_r; $this-&gt;img_h = $newHeight; $this-&gt;img_w = $newWidth; } function createFile($output_filename = null) { if($this-&gt;ext == "JPG" OR $this-&gt;ext == "JPEG" OR $this-&gt;ext == "PNG" OR $this-&gt;ext == "GIF") { imageJPEG($this-&gt;dst_r, $this-&gt;uploaddir.$output_filename.'.'."jpg", $this-&gt;quality); } /*elseif($this-&gt;ext == "PNG") { imagePNG($this-&gt;dst_r, $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext); } elseif($this-&gt;ext == "GIF") { imageGIF($this-&gt;dst_r, $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext); }*/ $this-&gt;output = $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext; } function setUploadDir($dirname) { $this-&gt;uploaddir = $dirname; } function flush() { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; imagedestroy($this-&gt;dst_r); unlink($targetFile); imagedestroy($this-&gt;img_r); } </code></pre> <p>And then for resizing I do:</p> <pre><code> $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-&gt;setFile($targetFile); $image-&gt;setUploadDir($targetPath); $image-&gt;resize(200); $image-&gt;createFile(md5($id)); $image-&gt;flush(); </code></pre> <p>I'm current using uploadify, but it uses this upload / resize script. Is there anyway to optimize this code so that it uses less memory?</p> <p>Thanks :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload