Jump to content

Recommended Posts

I have been trying to compress a set of images which are entered through a form. But I am not able to compress the images. The images are uploaded  in the 'gallery' folder but not in the 'thumbs' folder which stores the compressed images. Here is my code:

$tsrc = "upload/thumbs/".$_FILES["image_id"]["name"][$j];
  $imgfile = $_FILES["image_id"]["tmp_name"][$j];
  move_uploaded_file($imgfile , "upload/gallery/" . $_FILES["image_id"]["name"][$j]);
    list($width,$height)=getimagesize($imgfile); 
	echo $width; 
    	echo $height; 
    	$imgratio=$width/$height; 
	if($imgratio>1) 
	{ 
        	$newwidth=$thumbsize; 
        	$newheight=$thumbsize/$imgratio; 
   	    } else 
	{ 
       		 $newheight=$thumbsize;        
       		 $newwidth=$thumbsize*$imgratio; 
   	    } 
	$thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image 
    	$source=imagecreatefromjpeg($imgfile); // Now it will create a new image from the source 
    	imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);  // Copy and resize the image 
   	    imagejpeg($thumb,$tsrc,100); 

 

Please tell where i am wrong. Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/242058-need-help-in-compressing-images/
Share on other sites

Your post title is misleading. Compression refers to reducing the size of the file such that the original remains unchanged or with a loss of fidelity. What you are asking about is resizing an image.

 

Nothing in your code jumps out at me. Are you getting any errors?

move_uplaoded_file does what it's name indicates. It moves the uploaded file from the tmp/temp folder to the destination.

 

Because you are reading the tmp/temp file - $_FILES["image_id"]["tmp_name"][$j] to resize it after you have executed the move_uplaoded_file statement, the file no longer exists and you would be getting a message something like the following (along with a few other related messages), if you had php's error reporting and display errors turned on -

 

Warning: getimagesize(tmp\php1C.tmp) [function.getimagesize]: failed to open stream: No such file or directory in ...

 

In your previous thread on this, you were apparently trying to use the destination where you moved the uploaded file to as the source for the resize logic. Why did you change to using the tmp/temp image?

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.