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
https://forums.phpfreaks.com/topic/159741-can-this-image-script-be-optimized/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.