ryanfilard Posted November 4, 2011 Share Posted November 4, 2011 I already have a script that uploads the image but I would like to make a copy of the image resized down by 50% and uploaded to a new folder (imgups/thumbnails) I want it to do this all in one php script. I am not sure how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/250413-image-resizer/ Share on other sites More sharing options...
floridaflatlander Posted November 4, 2011 Share Posted November 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250413-image-resizer/#findComment-1284857 Share on other sites More sharing options...
Alkimuz Posted November 4, 2011 Share Posted November 4, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250413-image-resizer/#findComment-1284899 Share on other sites More sharing options...
The Little Guy Posted November 4, 2011 Share Posted November 4, 2011 If you have imagemagick you could make your life a lot easier and just do this: shell_exec("convert rose.jpg -resize 50% thumb/rose.png"); Quote Link to comment https://forums.phpfreaks.com/topic/250413-image-resizer/#findComment-1284946 Share on other sites More sharing options...
jcbones Posted November 4, 2011 Share Posted November 4, 2011 If you don't have imageMajik, I would suggest simple Image. One of the easiest free image classes out ATM. Quote Link to comment https://forums.phpfreaks.com/topic/250413-image-resizer/#findComment-1284970 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.