Jump to content

Image Resizer


ryanfilard

Recommended Posts

I use some code that's pretty good, I got it from the book PHP Solutions by david powers. The old one is real cheap on Amazon and there is also a pdf version for free online somewhere, you'll have to search. I can set the size to save, rename and I reduce the pixel count to 50 or what ever I want %. I use 50% and the images still look pretty good for online.

Link to comment
Share on other sites

i use the following for resizing to other size in pixels, you can change it a little to work for percentages (set in the main code the pixel size to, for example, 0.5 and define the new width and height in the function photoresize by multiplying the original width and height with this number)

 

this code will mentain the aspect ratio and use the with as main resize variable and adapt height to that, you can play with the beginning of the function photoresize to change that. 

 

notice the "copy()" in the uploadscript

 

in function.php:

<?php
function photoupload($filename, $source, $destination, $destination2)
{
/*START PHOTOUPLOAD*/
        
// Does the file have the right MIME type?

   if ($_FILES[$filename]['type'] != 'image/pjpeg' AND $_FILES[$filename]['type'] != 'image/jpeg' AND $_FILES[$filename]['type'] != 'image/gif' AND $_FILES[$filename]['type'] != 'image/png' AND $_FILES[$filename]['type'] != 'image/wbmp')
      {
      echo 'the file you want to upload is no photo, please go back to try again';
      exit;
      }

		  
      move_uploaded_file ($source, $destination );
		copy($destination, $destination2);

return 0;

/*END PHOTOUPLOAD*/
}

function photoresize($source, $maxx, $maxy)//foto wordt max breedte
{
/*START PHOTORESIZE*/

	// Get current dimensions

    list($width_orig, $height_orig) = getimagesize($source);


       // Check if they are over their limit

       if ( ($width_orig > $maxx) || ( $height_orig > $maxy))

          {

          if ($maxx && ($width_orig < $height_orig)) 
				  {
					$maxx = ($maxy / $height_orig) * $width_orig;
					} 
				else 
					{
					$maxy = ($maxx / $width_orig) * $height_orig;
					}
		   
		   // Resample voorbereiden
		   $image_p = imagecreatetruecolor($maxx, $maxy);
			 $image_type = strtolower( substr($source, strrpos( $source, '.' )) );

			 switch( $image_type )
			   {
				 case '.gif' :  $image = imagecreatefromgif($source);  break;
				 case '.jpg' :  $image = imagecreatefromjpeg($source); break;
				 case '.jpeg':  $image = imagecreatefromjpeg($source); break;
				 case '.png' :  $image = imagecreatefrompng($source);  break;
				 case '.bmp' :  $image = imagecreatefromwbmp($source); break;
				 }
		   // Resample de afbeelding

			 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $maxx, $maxy, $width_orig, $height_orig);

				 // Output
			 switch( $image_type)
			   {
				 case '.gif'  :  imagegif($image_p, $source, 100);  break;
				 case '.jpg'  :  imagejpeg($image_p, $source, 100); break;
				 case '.jpeg' :  imagejpeg($image_p, $source, 100); break;
				 case '.png'  :  imagepng($image_p, $source, 100);  break;
				 case '.bmp'  :  image2wbmp($image_p, $source, 100);break;
				 }
			  
         

        } 
	                                            



return 0;	

    

/*END PHOTORESIZE*/
}

?>	

 

in your main code:

<?php
@$picture = time().'-'.$_FILES['picture']['name'];

@$filename = 'picture';

if ($_FILES['picture']['tmp_name'] == TRUE)
   {
//photo uploaden
$source = $_FILES['picture']['tmp_name'];
$path= 'pictures/';

$destination = $path.$picture ;
photoupload($filename, $source, $destination);

//photo resizen
$source = $destination;
$maxx= '500';
$maxy= '500';
photoresize2($source, $maxx, $maxy);
   }
?>

 

hope this helps!

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.