doforumda Posted February 23, 2010 Share Posted February 23, 2010 hi i am trying to upload an image and resize it to thumbnail size. how can i resize that image which is being uploaded? i want to resize my image to width=50 and height=50 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="fileupload.php" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" /><p> <input type="submit" name="submit" value="Upload" /> </form> </body> </html> fileupload.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $name = $_FILES["myfile"]["name"]; $type = $_FILES["myfile"]["type"]; $size = $_FILES["myfile"]["size"]; $temp_name = $_FILES["myfile"]["tmp_name"]; $error = $_FILES["myfile"]["error"]; function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $ext = getExtension($name); $ext = strtolower($ext); if($error == 0) { $location = "upload/".$name; if($ext == "png" && $ext == "jpg" && $ext == "jpeg" && $ext == "gif") { if($size > 2000000) { if(!file_exists($location)) { move_uploaded_file($temp_name,$location); //query database to add location and image name. echo "Upload Complete."; } else die("File already exist."); } else die("format is not allowed or file size is too big"); } else die("it is too big size."); } else die("Error uploading file! code $error."); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/ Share on other sites More sharing options...
developerdave Posted February 23, 2010 Share Posted February 23, 2010 You coudn't resize the image as its being uploaded but once its on the server you can resize it using imagecopyresampled check the php.net site for the documentation on that or if you wanted to cheat you could download the WideImage PHP class (Google it) that has a very simple and easy to use set of functions its also capable of doing most other things with images. It is based around the GD library though so if your planning a big launch of anything I'd look into using Image Magick Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1016762 Share on other sites More sharing options...
bernardmoes Posted February 23, 2010 Share Posted February 23, 2010 im resizing my image too, you wanna know how? heres the class you will need: <?php class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> save this class as simpleimage.php and i did the same thing as you did, this is my upload page: <?php session_start(); include('SimpleImage.php'); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $upfile = $_FILES['uploadedfile']['name']; $fille = file_extension($upfile); $gebruikers = $_SESSION['inloggen']; if($fille == "jpg") { $target_path = "uploads/" . $gebruikers . ".jpg"; } elseif($fille == "png") { $target_path = "uploads/" . $gebruikers . ".png"; } elseif($fille == "gif") { $target_path = "uploads/" . $gebruikers . ".gif"; } if(file_exists($target_path)) { unlink($target_path); } if ($fille == "jpg" || $fille == "png" || $fille == "gif") { if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The image ". basename( $_FILES['uploadedfile']['name']). " has been uploaded successfully!!"; $image = new SimpleImage(); $image->load($target_path); $image->resize(50,50); $image->save($target_path); } else { echo "Don't make the image too big to upload!!"; } } else { echo "Wrong file! use jpg, png or gif!!"; } ?> i hope this works for you. it does work for me Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1016795 Share on other sites More sharing options...
doforumda Posted February 24, 2010 Author Share Posted February 24, 2010 now i am using imagecopyresized and my code is working fine. the problem only is when my image is resizes to thumbnail then its quality gets bad. Why is that and how can i maintain the quality of my image? <?php function imagetothumb($imagename, $extension) { $save = "thumb/".$imagename; $filename = $imagename;//'image.jpg'; $percent = 0.05; //$width = 50; //$height = 50; header('Content-type: image/'.$extension); list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb,$save,100); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $imagename = "image3.jpg"; $ext = getExtension($imagename); $ext = strtolower($ext); //imageresizer($imagename,$ext); imagetothumb($imagename,$ext); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017274 Share on other sites More sharing options...
developerdave Posted February 24, 2010 Share Posted February 24, 2010 You could try saying to PNG. Its a lossless format. change imagejpeg($thumb,$save,100); to imagepng($thumb,$save,0); 0 is 0 compression (lossless) See how that works for you Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017379 Share on other sites More sharing options...
doforumda Posted February 24, 2010 Author Share Posted February 24, 2010 now it is displaying this output "http://localhost/uploadfile/resizeimage.php" Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017385 Share on other sites More sharing options...
developerdave Posted February 24, 2010 Share Posted February 24, 2010 Thats a localhost link lol. I can't see anything. Have to paste what its returning. Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017387 Share on other sites More sharing options...
doforumda Posted February 24, 2010 Author Share Posted February 24, 2010 ok it is fine now its working but still same bad quality of the image Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017388 Share on other sites More sharing options...
developerdave Posted February 24, 2010 Share Posted February 24, 2010 You can play with the number at the end of the line I gave you (the max is 9) see what looks best. I use Image Magick so I can't remember if imagepng('output.png',0); is lossless or imagepng('output.png',9); but try adjusting that number see what you can do. Maybe post a link to an image you've uploaded? Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017390 Share on other sites More sharing options...
doforumda Posted February 24, 2010 Author Share Posted February 24, 2010 ok let me ask about image magick i also learn abit about that. What will i do if i use image magick and then upload that to webserver so what will i do i mean will to enable image magick on the webserver as it is out of my reach so what step should i take? Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017393 Share on other sites More sharing options...
developerdave Posted February 24, 2010 Share Posted February 24, 2010 Firstly you'll have to check your phpinfo(); to see if your web server supports it. Then you will need to decide whether you want to do it from a shell point of view or using a wrapper (MagickWand) to run it. I personally prefere using shell ie running it like this passthru('convert sourcepic.jpg -rotate 45 save_as_pic.png ',$out); And its as easy as that. But you do need shell access so try running it and maybe ask your hosting provider if they will enable it (if disabled) Quote Link to comment https://forums.phpfreaks.com/topic/193067-need-help-in-resizing-image/#findComment-1017403 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.