Jump to content

PHP Resize


ColdEdge

Recommended Posts

Alright, here is a question. Lets say one of my users uploads an avatar. By default my script will re-size the image into a smaller size and unlink the original file there for deleting it from the system. The problem that I am facing right not is that during the upload process there are 3 functions in the upload script. 1. Create Profile Picture, Profile Picture has a width of 225 and the height is calculate during the upload process.2. Create Thumbnail, Thumb has a width of 162 and the height is also calculated during the upload process.3. Create Mini Thumb, Mini Thumb is used for smaller parts of the side such as in mail/comment systems. This image has a width of 50 and height is also gained as done in the above functions: 1-2 that is.Now, I don't want to waste disk space on having 3 images avatar_1.jpg, t_avatar_1.jpg and a_avatar_1.jpg (this follow the function creation order above). Currently my script uploads images into 1 directory with 2 more sub directories.uploads/avatars/ (ex; uploads/avatars/avatar_1.jpg)uploads/avatars/thumbs/ (ex; uploads/avatars/thumbs/t_avatar_1.jpg)uploads/avatars/thumbs/small/ (ex; uploads/avatars/thumbs/small/a_avatar_1.jpg)The fact that bothers me is that I am using extra space for something that can be resized with use of PHP. I have to say that the thumb and mini thumb don't take that much space, but it adds up over time to equal entire avatar image which can be uploaded instead of creating additional images for it.Now, I seen a site that was shown to me by a friend allow you to upload an avatar. Sure there is nothing amazing in that as majority of the sites have some sort of display picture upload function.The thing thats different here is that it uses single index.php file to call image file via getImage= query, fetching the image id from the database server. Nothing special in that as well as I can do that my self with ease. The other part is that site uses is another query photoType= query. This query uses numbers and here what they doAfter and image has been uploaded and scaled to 320 x 480 pixels.1. photoType=2 -> 85 x 128 pixels2. photoType=4 -> 50 x 50 pixels3. photoType=5 -> 190 x 190 pixelsThats about it, if you put in 6,7,8 and ect it will just display original image with dimensions of 320 x 480 pixels.So here is how the original image looks (320 x 480 pixels)j415536m2wF57cFx706vWL3ZtyEo7JZ774izV6CF41C8UsA5i5l2C0VJiB73.jpgAnd here is how it looks with photoType=5 query attached to it. Dimensions become 190 x 190 pixels.11x002LooLFz611yrMJvC5R272w60u1G5u995tYP796VBlIp1QFtk6160244.jpgAs you can see PHP has taken the top part of the image and outputted it using the photoType query. I don't think the site creates thumbnails or anything, but I can't get how this works. I tried something like this, but my load time is two massive and all my script dose is resizes a file from say 1900 x 1200 to something smaller like 490 x something. But this script used by the site gets original image and creates a avatar which is 190 x 190 pixels. During the creation only top part is pretty much used.Can some one tell me how can I resize the image and display it like that from the original file using a query?- Thank you in advance.

Link to comment
Share on other sites

It's the same thing as resizing it initially, they just send the image to the browser instead of saving it. Based on the querystring they know which file to resize and the dimensions, so they just resize it and send it to the browser. Doing images this way will be much more memory and CPU-intensive (and therefore slower) than saving the images once and linking to them. Especially if you have 10 or more images on a page that PHP needs to resize every time, and your server isn't very fast, you'll be able to load the page and watch the individual images start to pop up one at a time.

Link to comment
Share on other sites

Ok, so I will stick with my original script just put the JPG quality a bit down say to 90 or so.Also here is a problem, when I upload a picture even so the config.php tells the function.php script to make the image 192 x 192 what ends up happening is the entire image is re-sized. So looks like this11x002LooLFz611yrMJvC5R272w60u1G5u995tYP796VBlIp1QFtk6160244.jpg[should be Like this]tavatar3.jpg[Looks like...]This happens when I resize the original in my script (320 x 480 pixels)j415536m2wF57cFx706vWL3ZtyEo7JZ774izV6CF41C8UsA5i5l2C0VJiB73.jpgScript

function createThumbnail($new_file) {    	require 'config.php'; 	  	if(preg_match('/[.](jpg)$/', $new_file)) {  		$im = imagecreatefromjpeg($path_to_image_directory . $new_file); 		$ox = imagesx($im);  		$oy = imagesy($im);    		$nx = $final_width_of_image;  		$ny = $final_width_of_image;   		$nm = imagecreatetruecolor($nx, $ny);		imagealphablending($im, false);	  		imagecopyresampled($nm, $im, 0, 0, 0, 0,$nx,$ny,$ox,$oy);  		imagealphablending($im, true);		if(!file_exists($path_to_thumbs_directory)) {  	  if(!mkdir($path_to_thumbs_directory)) {  		   die("There was a problem. Please try again!");  	  }  	   }    	imagejpeg($nm, $path_to_thumbs_directory . "t_" . $new_file, 100);		$t = time();			$tn = '<img src="' . $path_to_thumbs_directory . "t_" . $new_file . '?dateline='.$t.'" alt="image" />';

How can I make PHP select top part of the image instead of creating a thumb of the original image?

Link to comment
Share on other sites

The imagecopyresampled function takes the box to copy to the other image, so just change the position and dimensions of that box. Don't copy all the way to the original width and height if you don't want all of that data.

Link to comment
Share on other sites

this seems to do the trick

function createThumbnail($new_file) {    	require 'config.php'; 	  	if(preg_match('/[.](jpg)$/', $new_file)) {  		$im = imagecreatefromjpeg($path_to_image_directory . $new_file); 		$ox = imagesx($im);  		$oy = imagesy($im);    		$nx = 190;  		$ny = 190;   		$nm = imagecreatetruecolor($nx, $ny);		imagealphablending($im, false);	  		imagecopyresampled($nm, $im, 0, 0, 0, 0, $nx,$ny,$ox,$oy - 158);  		imagealphablending($im, true);		if(!file_exists($path_to_thumbs_directory)) {  	  if(!mkdir($path_to_thumbs_directory)) {  		   die("There was a problem. Please try again!");  	  }  	   }    	imagejpeg($nm, $path_to_thumbs_directory . "t_" . $new_file, 87);		$t = time();			$tn = '<img src="' . $path_to_thumbs_directory . "t_" . $new_file . '?dateline='.$t.'" alt="image" />';

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...