Jump to content

Can this image script be optimized?


noj75

Recommended Posts

Greetings all,

 

I am struggling with a script on my server that is using alot of memory. I have upped the max file size and the memory limit to what my hosts will allow me to use.

 

The code below is the main "feature" script of the whole thing. I am wondering if there is a way to optimize it at all. My clients NEED to upload photos, some over 2-3MB.

 

$image = $_FILES["new_image"]["name"];
$uploadedfile = $_FILES['new_image']['tmp_name'];

/* START DEALING WITH THE UPLOADED IMAGE */
if ($image) {

	$filename  = stripslashes($_FILES['new_image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);

	$uploadedfile = $_FILES['new_image']['tmp_name'];
	$src = imagecreatefromjpeg($uploadedfile);

	list($width,$height) = getimagesize($uploadedfile);

	/* SET LARGE PHOTO SIZES */
		$newwidth=600;
		$newheight=450;
	$tmp = imagecreatetruecolor($newwidth,$newheight);

	/* SET THUMBNAIL SIZES */
		$newwidth1=250;
		$newheight1=188;
	$tmp1 = imagecreatetruecolor($newwidth1,$newheight1);

	/* COPY THE IMAGES */
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

	imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

	/* RENAME THE PHOTOS */
	$newimagename  =  $hname.'.'.$extension;
	$newimagename1 =  $hname.'.'.$extension;

	/* STORE THE IMAGE AND THUMBNAIL */
	$filename  = "../../****/images/****/". $newimagename;
	$filename1 = "../../****/images/****/thumbs/". $newimagename1;

	imagejpeg($tmp,$filename,100); imagejpeg($tmp1,$filename1,100);

	imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1);

}

 

Any help appreciated as I am working on abit of a deadline. Many thanks.

Link to comment
Share on other sites

I agree with "thebadbad".

You appear to be creating 3 images in memory (the original, and 2 tmps).

If you are deleting the original why don't you just alter that memory image directly and save it before remove the tmp.

Also put it all into a function, something like:

 

function createResizedJpeg($src, $dst, $toWidth, $toHeight){
  static $srcFile;
  static $srcResource;
  static $srcWidth;
  static $srcHeight;


  // If we don't have a cached file source create a new one.
  if(is_null($srcFile) OR $src !== $srcFile){
     $srcFile = $src;
     $srcResource = imagecreatefromjpeg($src);
     list($srcWidth,$srcHeight) = getimagesize($srcFile);
  }

  // Create tmp.
  $tmp = imagecreatetruecolor($toWidth, $toheight);

  // Resize
  imagecopyresized($tmp, $srcResource, 0, 0, 0, 0, $toWidth, $toheight, $srcWidth, $srcHeight);
  imagejpeg($tmp,$dst,100);
  imagedestroy($tmp);
}

 

I make no guarantees the above code will be any better but to make sure we're not recreating the src file resource it uses a simple caching technique.

You may wish to put in additional checks to make sure the src image filename is actually a jpeg.

Link to comment
Share on other sites

Hi guys,

 

Thanks for the replies. I am not too great with PHP at present. Could anyone give me an example on how to re-arrange the code to deal with the large image first and then the small thumb.

 

Many thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.