kirkh34 Posted May 16, 2011 Share Posted May 16, 2011 I'm trying to find a good function that allows me to resize an image and specify the paths, the dimensions, and the quality about the image. Does anyone have any suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/236503-image-resize-function/ Share on other sites More sharing options...
JasonLewis Posted May 16, 2011 Share Posted May 16, 2011 No one is going to throw something at you like that. You'd need to take advantage of the GD library. A quick Google search comes up with a few things: http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/ < this one uses OOP from a quick glance http://www.phptoys.com/e107_plugins/content/content.php?content.46 It's something you're just going to have to attempt to do yourself. Those aren't the only two tutorials either, there are a heap out there. Just look around, if you have any more problems let us know. Quote Link to comment https://forums.phpfreaks.com/topic/236503-image-resize-function/#findComment-1215876 Share on other sites More sharing options...
dragon_sa Posted May 16, 2011 Share Posted May 16, 2011 Here is something to try, it is what I use, you submit the image from a form in this example, the form field passed in this example is named "logo" and can add a database insert or update at the end depending on your needs <?php include database connection here // Process new image if ($_FILES['logo']['tmp_name']&&$_FILES['logo']['tmp_name']!="") { // set memory limit for image ini_set("memory_limit","32M"); // Check file extension $checkfile=$_FILES['logo']['name']; $ext=substr($checkfile, -3); // Redirect if not correct image type if (!in_array($ext, array('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG'))) { header("Location: redirect to some page if wrong file type"); // Close database connection mysql_close($link); exit (); } // Create new image (300px wide can modify to suit height if that is preferred or set both but not advised) $file=$_FILES['logo']['tmp_name']; list($width, $height)=getimagesize($file); // Scale to width $main_width=300; $main_height=$height*($main_width/$width); if ($ext=="jpg"||$ext=="JPG") { $image=imagecreatefromjpeg($file); $newname="image".time().".jpg"; // using time as part of the name guarantees a unique name for each new image to prevent duplicate naming } if ($ext=="gif"||$ext=="GIF") { $image=imagecreatefromgif($file); $newname="image".time().".gif"; } if ($ext=="png"||$ext=="PNG") { $image=imagecreatefrompng($file); $newname="image".time().".png"; } // Create main image file $main_image=imagecreatetruecolor($main_width, $main_height); imagecopyresampled($main_image, $image, 0, 0, 0, 0, $main_width, $main_height, $width, $height); $cp_file="some/path/".$newname; $site_file="another/path/".$newname; if ($ext=="jpg"||$ext=="JPG") { imagejpeg($main_image, $cp_file, 100); // 100 here is the max quality setting for jpeg imagejpeg($main_image, $site_file, 100); } if ($ext=="gif"||$ext=="GIF") { imagegif($main_image, $cp_file, 100); // 100 here is the max quality setting for gif imagegif($main_image, $site_file, 100); } if ($ext=="png"||$ext=="PNG") { // Turn off alpha blending and set alpha flag imagepng($main_image, $cp_file, 9); // 9 here is the max setting for png quality imagepng($main_image, $site_file, 9); } save file name to database here with mysql insert or update query using variable $newname as the name or you can optionally save path aswell using the variable $cp_file and $site_file ?> Quote Link to comment https://forums.phpfreaks.com/topic/236503-image-resize-function/#findComment-1215909 Share on other sites More sharing options...
Sanjib Sinha Posted May 16, 2011 Share Posted May 16, 2011 My suggestion is before posting this type of coding help here you need to google it out. There are so many codes available over the net that you'd get almost overwhelmed. Best of luck. Quote Link to comment https://forums.phpfreaks.com/topic/236503-image-resize-function/#findComment-1215914 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.