Lefteris Posted June 13, 2008 Share Posted June 13, 2008 Hello all, I have a problem. I can't resize images with imagecopyresampled. Here is my code : .... .... //if the size of the image is greater than 200 kbs if($_FILES['userpic']['size'] > 204800) die("Please upload an image with size lower than 200 Kbs"); //get its dimensions $dimensions = getimagesize($_FILES['userpic']['tmp_name']); //if the dimensions don't fit then resize it! if($dimensions['width'] > 150 || $dimensions['height'] > 150) { imagecopyresampled($_FILES['userpic']['tmp_name'],$_FILES['userpic']['tmp_name'],0,0,0,0,150,150,$dimensions['width'],$dimensions['height']); } $fcontents = file_get_contents($_FILES['userpic']['tmp_name']); //store the image in the db $sql->updatetable("users","userImage","'".mysql_real_escape_string($fcontents)."'","username","'".$username."'"); //and its type $sql->updatetable("users","userImageType","'".$extension."'","username","'".$username."'"); ... ... Shouldn't this work? It is not working. Actually ... it worked a little ... but the resizing was completely out of proportion. It should get resized to 150x150 but it got resized to 280x212 pixels. Any idea why? Also what is the appropriate way to resize an image? Thank in advance. Link to comment https://forums.phpfreaks.com/topic/110056-solved-resizing-images-with-imagecopyresampled/ Share on other sites More sharing options...
Lefteris Posted June 13, 2008 Author Share Posted June 13, 2008 Sorry for bumping my own topic but since it got only 4 views I assume it got buried due to lots of new topics and none got a change to see it. So ... anyone got any idea how to properly resize an image with php? Why is my way wrong? Link to comment https://forums.phpfreaks.com/topic/110056-solved-resizing-images-with-imagecopyresampled/#findComment-564822 Share on other sites More sharing options...
craygo Posted June 13, 2008 Share Posted June 13, 2008 Here is some code you can change http://www.phpfreaks.com/forums/index.php/topic,191541.0.html Ray Link to comment https://forums.phpfreaks.com/topic/110056-solved-resizing-images-with-imagecopyresampled/#findComment-564825 Share on other sites More sharing options...
Lefteris Posted June 13, 2008 Author Share Posted June 13, 2008 Thanks a lot friend, but I still have a problem. Turned out I had errors in the above code so it never went to the imagecopyresampled part. In particular I never checked the width and height of the image correctly. I tried to do it as I had done but it did not accept $_FILES['userpic']['tmp_name'] as either a destination image or a source image for the image copy resampled. So I then tried it your way, like in the script you linked but a lot simpler since I just want to resize everything to 150x150. The problem is ... how do you store this image in the database? I used to store images in the database by doing something like that : $fcontents = file_get_contents($img_dst); and then adding it with mysql_real_escape_string($fcontents) But now img_dst is not a filename somewhere in the server, it is an image resource. How do you store it in the database? Moreover I can't check to see if the resizing took place if I don't store it in the DB since I already have headers on the top of the page and can't see the image with a simple imagejpeg($img_dst). Here is the code : //get its dimensions $dimensions = getimagesize($_FILES['userpic']['tmp_name']); $imgwidth = $dimensions[0]; $imgheight = $dimensions[1]; //if the dimensions don't fit then resize it! if($imgwidth != 150 || $imgheight != 150) { $img_dst=imagecreatetruecolor(150,150); $img_src = imagecreatefrompng($_FILES['userpic']['tmp_name']); //resize using this ultra cool function! Really cool! imagecopyresampled($img_dst,$img_src,0,0,0,0,150,150,$imgwidth,$imgheight); } Link to comment https://forums.phpfreaks.com/topic/110056-solved-resizing-images-with-imagecopyresampled/#findComment-564987 Share on other sites More sharing options...
craygo Posted June 13, 2008 Share Posted June 13, 2008 Storing the image itself is not a good idea. I have had nothing but problem doing it. I prefer to store the image name or the name and path in the database and just use html to display the picture when needed. In any case, to store your image you would have to first save the image to the drive, then insert it into the database, then delete the file. You can save the image by adding a parameter to imagejpeg $path = "absolute/path/with/name.jpg"; imagejpeg($img_dst, $path); Ray Link to comment https://forums.phpfreaks.com/topic/110056-solved-resizing-images-with-imagecopyresampled/#findComment-564996 Share on other sites More sharing options...
Lefteris Posted June 13, 2008 Author Share Posted June 13, 2008 Aha I see. Thanks a lot, makes sense that I would have to save it somewhere. Sounds like a lot of trouble but I feel really organized by having user images in the db instead of in a file. I will try to implement it and post back when I do. Thanks again. Link to comment https://forums.phpfreaks.com/topic/110056-solved-resizing-images-with-imagecopyresampled/#findComment-564999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.