DaveLinger Posted July 5, 2006 Share Posted July 5, 2006 Hello everyone,I'm very familiar with PHP but NOT very familiar with the GD image functions.I'd like to upload an image, have it resized to, say, 150x150 (not exactly that, I'd like it to make it 150 by whatever the other dimension needs to be for it to be proportional), then saved as imagename_thumbnail.whatever . I can handle the file upload and pass the file url to the next php page, but where do I go from there?Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/ Share on other sites More sharing options...
litebearer Posted July 5, 2006 Share Posted July 5, 2006 This might be of some help...http://www.nstoia.com/toh/technical/imageresize/index.phpLite... Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53145 Share on other sites More sharing options...
DaveLinger Posted July 5, 2006 Author Share Posted July 5, 2006 VERY helpful. Thanks![code]<?phpfunction Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { $s_path = trim($s_path); $o_path = trim($o_path); $save = $s_path . $save; $file = $o_path . $file; $ext = strtolower(end(explode('.',$save))); list($width, $height) = getimagesize($file) ; if(($width>$t_w) OR ($height>$t_h)) { $r1 = $t_w/$width; $r2 = $t_h/$height; if($r1<$r2) { $size = $t_w/$width; }else{ $size = $t_h/$height; } }else{ $size=1; } $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($file) ; break; case 'gif': $image = imagecreatefromgif($file) ; break; case 'png': $image = imagecreatefrompng($file) ; break; } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; return;}####################### Example usage######################$save = 'myfile.jpg';$file = 'original.jpg';$t_w = 120;$t_h = 120;$o_path = " ";$s_path = " ";Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53154 Share on other sites More sharing options...
DaveLinger Posted July 5, 2006 Author Share Posted July 5, 2006 err...[code]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="2048000">File: <input name="userfile" type="file" /><br /><input type="submit" value="Upload" /></form><?php// Here's Where I define the Resize_Image function //function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { $s_path = trim($s_path); $o_path = trim($o_path); $save = $s_path . $save; $file = $o_path . $file; $ext = strtolower(end(explode('.',$save))); list($width, $height) = getimagesize($file) ; if(($width>$t_w) OR ($height>$t_h)) { $r1 = $t_w/$width; $r2 = $t_h/$height; if($r1<$r2) { $size = $t_w/$width; }else{ $size = $t_h/$height; } }else{ $size=1; } $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($file) ; break; case 'gif': $image = imagecreatefromgif($file) ; break; case 'png': $image = imagecreatefrompng($file) ; break; } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; return;}// Here's where it checks to see if it's uploaded, and starts an if statement //if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {copy($_FILES["userfile"]["tmp_name"], "../files/" . $_FILES["userfile"]["name"]);// Since the image was uploaded, let's create that thumbnail //$save = 'myfile.jpg';$file = "$_FILES['userfile']['name']";$t_w = 120;$t_h = 120;$o_path = "files";$s_path = "files/thumbnails";Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);// otherwise, just err //}else{echo "upload error";}?>[/code]"Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/s/b/o/sboyce1/html/photos/files/upload.php on line 52"it gives me that on page load... doesnt even show the form... Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53159 Share on other sites More sharing options...
DaveLinger Posted July 5, 2006 Author Share Posted July 5, 2006 got it.[code]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="2048000">File: <input name="userfile" type="file" /><br /><input type="submit" value="Upload" /></form><?phpif (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {copy($_FILES["userfile"]["tmp_name"], "files/" . $_FILES["userfile"]["name"]);$uploadedimage = $_FILES['userfile']['name'];function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { $s_path = trim($s_path); $o_path = trim($o_path); $save = $s_path . $save; $file = $o_path . $file; $ext = strtolower(end(explode('.',$save))); list($width, $height) = getimagesize($file) ; if(($width>$t_w) OR ($height>$t_h)) { $r1 = $t_w/$width; $r2 = $t_h/$height; if($r1<$r2) { $size = $t_w/$width; }else{ $size = $t_h/$height; } }else{ $size=1; } $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($file) ; break; case 'gif': $image = imagecreatefromgif($file) ; break; case 'png': $image = imagecreatefrompng($file) ; break; } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; return;}$save = 'myfile.jpg';$file = $uploadedimage;$t_w = 120;$t_h = 120;$o_path = "files/";$s_path = "files/thumbnails/";Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);echo "w00t.";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53168 Share on other sites More sharing options...
DaveLinger Posted July 5, 2006 Author Share Posted July 5, 2006 hokay. Also trying to apply text as a watermark before the resizing, so it appears on both images at proportional size.Not working:[code]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="2048000">File: <input name="userfile" type="file" /><br /><input type="submit" value="Upload" /></form><?phpif (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {copy($_FILES["userfile"]["tmp_name"], "files/" . $_FILES["userfile"]["name"]);$uploadedimage = $_FILES['userfile']['name'];function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { $s_path = trim($s_path); $o_path = trim($o_path); $save = $s_path . $save; $file = $o_path . $file; $ext = strtolower(end(explode('.',$save))); list($width, $height) = getimagesize($file) ; if(($width>$t_w) OR ($height>$t_h)) { $r1 = $t_w/$width; $r2 = $t_h/$height; if($r1<$r2) { $size = $t_w/$width; }else{ $size = $t_h/$height; } }else{ $size=1; } $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($file) ; break; case 'gif': $image = imagecreatefromgif($file) ; break; case 'png': $image = imagecreatefrompng($file) ; break; } // watermarking //$white = imagecolorallocate($image, 255, 255, 255);$text = 'MyText';$font = 'CALIBRI.TTF';imagettftext($image, 10, 0, 10, 10, $white, $font, $text);imagejpeg($image, $save, 100);// done watermarking // imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; return;}$save = $uploadedimage;$file = $uploadedimage;$t_w = 120;$t_h = 120;$o_path = "files/";$s_path = "files/thumbnails/";Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);echo "w00t.<br><br><img src=\"files/";echo $uploadedimage;echo "\"><br><br><img src=\"files/thumbnails/";echo $uploadedimage;echo "\">";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53288 Share on other sites More sharing options...
DaveLinger Posted July 5, 2006 Author Share Posted July 5, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53508 Share on other sites More sharing options...
wildteen88 Posted July 5, 2006 Share Posted July 5, 2006 Stop bumping topics that are only a few hours old. Please wait for at least 4 - 12 hours before bumping a topic again. I have removed your last two replies. Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53510 Share on other sites More sharing options...
DaveLinger Posted July 6, 2006 Author Share Posted July 6, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53663 Share on other sites More sharing options...
DaveLinger Posted July 6, 2006 Author Share Posted July 6, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53813 Share on other sites More sharing options...
litebearer Posted July 6, 2006 Share Posted July 6, 2006 Sorry been in meetings all week.Let me work on this tonight and see if I can scratch together a solution for you.Lite... Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-53838 Share on other sites More sharing options...
DaveLinger Posted July 6, 2006 Author Share Posted July 6, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-54158 Share on other sites More sharing options...
DaveLinger Posted July 7, 2006 Author Share Posted July 7, 2006 bump :-[ Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-54341 Share on other sites More sharing options...
kopander Posted July 7, 2006 Share Posted July 7, 2006 Don't know if this is any help, but found a script which can be used to add text the the center of an image (i.e. watermark). Perhaps you could get some ideas from there how to get your code working. Im quite new to GD stuff myself so. The link is:[url=http://www.nuonce.net/code-library/1084673172.html]http://www.nuonce.net/code-library/1084673172.html[/url] Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-54353 Share on other sites More sharing options...
DaveLinger Posted July 8, 2006 Author Share Posted July 8, 2006 well I already know how to watermark it, I just dont know how to get one script to upload, save, watermark, resize, then save as. =/ Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-54650 Share on other sites More sharing options...
Barand Posted July 8, 2006 Share Posted July 8, 2006 Sequence would be something like this[code]move_uploaded_file($_FILE['uploadedfile']['tmp_name'], 'mybigpic.jpg');$big = imagecreatetruecolor ('mybigpic.jpg'); // watermark code // now saveimagejpeg($big, 'mybigpic.jpg', 100);$small = imagecreatetruecolor (150, 150); // resize code from big to small // save small imageimagejpeg($small, 'mysmallpic.jpg', 75);imagedestroy($big);imagedestroy($small);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-54721 Share on other sites More sharing options...
hvle Posted July 8, 2006 Share Posted July 8, 2006 I have a class which you specify a maximum width, say 150px. This class then create a thumbnail images of the original image. Only that if width > height, then width will be 150 and height will be whatever proportional. if height > width, then height will be 150 and width will be.. u know. Thumb created is proportional, not stretched nor truncated.If you need this class, i'll post if up or send to you. Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-54732 Share on other sites More sharing options...
DaveLinger Posted July 10, 2006 Author Share Posted July 10, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-55534 Share on other sites More sharing options...
Barand Posted July 10, 2006 Share Posted July 10, 2006 You say you know how to add watermarks.You have sample resize code posted earlier in the threadI've shown you how to put it together.What do you want now? Quote Link to comment https://forums.phpfreaks.com/topic/13694-how-to-resize-an-image-and-save-as-new-file-with-gd-not-solved/#findComment-55714 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.