clown[NOR] Posted May 17, 2007 Share Posted May 17, 2007 I'm working on a registration form and you must upload a photo. Then the photo the person uploads must be copied and resized to 60x60... the upload part is working, it's the resize part that is acting up... this is the code I'm using <?php function resizeImage($image) { $imgSize = getimagesize($image); $imgWidth = $imgSize[0]; $imgHeight = $imgSize[1]; $newWidth = "60"; $newHeight = "60"; $source = imagecreatefromjpeg($image); $thumb = imagecreatetruecolor($newWidth, $newHeight); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight); imagejpeg($thumb); } ?> any ideas what I'm doing wrong?... this is the error i get ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC $.' ",#(7),01444' =82<.342ÿÛC 2!! ÿÀ<<"ÿÄ ÿĵ there's alot more weird symbols in the error message, but that's all the was stored when I copy/paste it... Thanks In Advance - Clown Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/ Share on other sites More sharing options...
Lumio Posted May 17, 2007 Share Posted May 17, 2007 <?php function resizeImage($image) { $imgSize = getimagesize($image); $imgWidth = $imgSize[0]; $imgHeight = $imgSize[1]; $newWidth = "60"; $newHeight = "60"; $source = imagecreatefromjpeg($image); $thumb = imagecreatetruecolor($newWidth, $newHeight); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight); header('Content-type: image/jpeg'); // <-- you need that! imagejpeg($thumb); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255668 Share on other sites More sharing options...
clown[NOR] Posted May 17, 2007 Author Share Posted May 17, 2007 awsome man!... thanks!! can you belive it... I've been sending whole last night, and i missed 1 bloody line.. oh well... now over to another problem... how can I now, move that image to the thumbs folder? Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255679 Share on other sites More sharing options...
phast1 Posted May 17, 2007 Share Posted May 17, 2007 Here is the code that I normally use to resize an image with good quality, plus JPEG optimization: // create thumbnail image, resized and optimized $destimg=ImageCreateTrueColor(60,60) or die("Problem In Creating image"); $srcimg=ImageCreateFromJPEG($binFile) or die("Problem In opening Source Image"); ImageCopyResampled($destimg,$srcimg,0,0,0,0,60,60,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing"); ob_start(); imagejpeg($destimg, '', 35); $thumb_data = addslashes(ob_get_contents()); ob_end_clean(); imagedestroy($destimg); imagedestroy($srcimg); After that, your $thumb_data var would have the binary data for the image and you can either save it into a database or write it to a file using fwrite().. The above code was intended to save into a database, so there might be an easier way to save directly to a file.. Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255722 Share on other sites More sharing options...
clown[NOR] Posted May 17, 2007 Author Share Posted May 17, 2007 hmm... so what you're saying is that... I should have a table named something like "news_userimg" and store all info to that? another question: how is the best way to scale the image so it looks good? cuz resizing a 250x100 to 60x60 will look really bad... Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255739 Share on other sites More sharing options...
Lumio Posted May 17, 2007 Share Posted May 17, 2007 <?php function resizeImage($image) { $imgSize = getimagesize($image); $imgWidth = $imgSize[0]; $imgHeight = $imgSize[1]; $newWidth = "60"; $newHeight = "60"; $source = imagecreatefromjpeg($image); $thumb = imagecreatetruecolor($newWidth, $newHeight); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight); $target = './thumbnails/'.$imagename; return imagejpeg($thumb, $target); // this is how you save an picture } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255769 Share on other sites More sharing options...
phast1 Posted May 17, 2007 Share Posted May 17, 2007 Deciding to store images in a database all depends on the situation.. If you do decide to, then yes, you should make an images table with fields such as id, filename, data, modified .. Make sure that the data field is binary.. As for getting the highest quality resize possible, please see my previous post that uses imagecopyresampled() instead of imagecopyresized() Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255812 Share on other sites More sharing options...
Lumio Posted May 17, 2007 Share Posted May 17, 2007 jep... imagecopyresampled is better. Use that to create thumbnails. Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255832 Share on other sites More sharing options...
clown[NOR] Posted May 18, 2007 Author Share Posted May 18, 2007 ok.. let me see if i got it... use imagecopyresampled() not imagecopyresized() as for the database Fields: - id - filename - data <-- this has to be binary? what do I store here? - modified <-- what do I store here? should I also add these fields? - width - height Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255989 Share on other sites More sharing options...
trq Posted May 18, 2007 Share Posted May 18, 2007 I would recommend just storing the filepath in mysql, not the file itself. Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255994 Share on other sites More sharing options...
clown[NOR] Posted May 18, 2007 Author Share Posted May 18, 2007 what do you mean? sorry for all these question, but never done this before Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-255998 Share on other sites More sharing options...
trq Posted May 18, 2007 Share Posted May 18, 2007 What do you mean what do I mean? Do you know what a filepath is? Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-256001 Share on other sites More sharing options...
clown[NOR] Posted May 18, 2007 Author Share Posted May 18, 2007 not sure... but I belive it's like "images/uploads/" or do I include the filename also? like "images/uploads/filename.jpg" Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-256015 Share on other sites More sharing options...
phast1 Posted May 18, 2007 Share Posted May 18, 2007 The 'filename' and 'modified' fields are optional, but they would typically be used to keep track of a filename to represent the image and the time that the image was last modified (this would be a TIMESTAMP field).. And yes, the 'data' field must be binary in order to store the image data.. I'm not sure why you would need to store the whole filepath, since the image will be in the database and there is no physical disk path for it.. As for height and width, you could store them to make it easier to generate afterwards instead of detecting the height and width each time, but if you are already resizing to a specific image size, then you probably already know the size and can just hard code it on the output part of the code.. Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-256094 Share on other sites More sharing options...
clown[NOR] Posted May 18, 2007 Author Share Posted May 18, 2007 thanks for all help here guys... very much appriciated... i consider this topic as solved now... Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-256313 Share on other sites More sharing options...
Lumio Posted May 19, 2007 Share Posted May 19, 2007 It would really be better to store files NOT in the database Quote Link to comment https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/#findComment-256931 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.