kclark Posted February 11, 2011 Share Posted February 11, 2011 I am using the following code to upload images for my products. How can I modify it to not only upload the product image, but upload it a 2nd time resized for thumbnail or is it even possible? if ($_FILES['file']['error'] > 0) { echo "Return Code: " . $_FILES['file']['error'] . "<br />"; } else { echo "Upload: " . $_FILES['file']['name'] . "<br />"; echo "Type: " . $_FILES['file']['type'] . "<br />"; echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES['file']['tmp_name'] . "<br />"; if (file_exists("images/" . $_FILES['file']['name'])) { echo $_FILES['file']['name'] . " already exists. "; } else { move_uploaded_file($_FILES['file']['tmp_name'], "images/$companyname/$materialdir/$prefix$modelnumber.jpg"); //"images/" . $companyname . "/" . $materialdir/$prefix$modelnumber . ".jpg"); //"images/" . $_FILES['file']['name']);//change to Timp folder // "C:/upload/" . $_FILES['file']['name']);//change to Timp folder //echo "Stored in: " . "C:/xampp/htdocs/Timp/UserLogin/upload/" . $_FILES["file"]["name"]; echo "Stored in: images/$companyname/$materialdir/$prefix$modelnumber.jpg"; } } $photo = "images/$companyname/$materialdir/$prefix$modelnumber.jpg"; Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/ Share on other sites More sharing options...
litebearer Posted February 11, 2011 Share Posted February 11, 2011 Perhaps... http://www.nstoia.com/sat/resize/ Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1172742 Share on other sites More sharing options...
kclark Posted February 11, 2011 Author Share Posted February 11, 2011 that tells me how to resize, but how can I upload 1 file and use it twice in the same upload Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1172926 Share on other sites More sharing options...
gizmola Posted February 11, 2011 Share Posted February 11, 2011 that tells me how to resize, but how can I upload 1 file and use it twice in the same upload You don't. You upload it, save it, then use that file to generate any thumbnails you need and store those either in a different directory with the same name or with a slightly varied name like "tn_file.jpg". Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1172936 Share on other sites More sharing options...
litebearer Posted February 11, 2011 Share Posted February 11, 2011 run it twice - if full image is tooo large , resize to agreeable size, then run 2nd time to make thumb ie if image upload width > maxwidth or upload height > maxheight - resize once save in images folder if makethumb=true resize to max thumb w & h save in thumbs folder Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1172938 Share on other sites More sharing options...
kclark Posted February 11, 2011 Author Share Posted February 11, 2011 How do I do this? You upload it, save it, then use that file to generate any thumbnails you need and store those either in a different directory with the same name or with a slightly varied name like "tn_file.jpg". Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1172971 Share on other sites More sharing options...
gizmola Posted February 11, 2011 Share Posted February 11, 2011 People typically use either the gd extension or the imagemagick one. I've only used gd, and find it's the most commonly installed extension. It's really very simple function based code you need: http://www.php.net/manual/en/function.imagecopyresampled.php You simply add that after you've moved the file, and have it save out as your thumbnail, exactly as illustrated in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1172979 Share on other sites More sharing options...
litebearer Posted February 12, 2011 Share Posted February 12, 2011 copy this into a file named resize.php <?PHP /* RESIZE FUNCTION */ /* the parameters */ /* $file is the name of the original image WITHOUT the path $save is the name of the resized image WITHOUT the path $t_w is the MAXIMUM width of the new image $t_h is the MAXIMUM height of the new image $o_path is the path to the original image INCLUDE trailing slash $s_path is the path where you want to save the new image INCLUDE trailing slash NOTE: to overwrite the original with the new, use the same name and path for both */ 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; } /* END OF RESIZE FUNCTION */ ?> here is your script with the function being used (READ the comments) <?PHP /* put this at the top of your upload script */ include('resize.php'); if ($_FILES['file']['error'] > 0) { echo "Return Code: " . $_FILES['file']['error'] . "<br />"; }else{ echo "Upload: " . $_FILES['file']['name'] . "<br />"; echo "Type: " . $_FILES['file']['type'] . "<br />"; echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES['file']['tmp_name'] . "<br />"; if (file_exists("images/" . $_FILES['file']['name'])){ echo $_FILES['file']['name'] . " already exists. "; }else{ move_uploaded_file($_FILES['file']['tmp_name'], "images/$companyname/$materialdir/$prefix$modelnumber.jpg"); //"images/" . $companyname . "/" . $materialdir/$prefix$modelnumber . ".jpg"); //"images/" . $_FILES['file']['name']);//change to Timp folder // "C:/upload/" . $_FILES['file']['name']);//change to Timp folder //echo "Stored in: " . "C:/xampp/htdocs/Timp/UserLogin/upload/" . $_FILES["file"]["name"]; echo "Stored in: images/$companyname/$materialdir/$prefix$modelnumber.jpg"; /* define the resizing parameters */ /* $file is the name of the original image WITHOUT the path */ $file = $prefix . $modelnumber . "jpg"; /* $save is the name of the resized image WITHOUT the path */ $save = $prefix . $modelnumber . "_rs.jpg"; /* $t_w is the MAXIMUM width of the new image */ $t_w = 800; /* $t_h is the MAXIMUM height of the new image */ $t_h = 600; /* $o_path is the path to the original image INCLUDE trailing slash */ $o_path = "images/" , $companyname . "/" . $materialdir . "/"; /* $s_path is the path where you want to save the new image INCLUDE trailing slash */ $o_path = "images/" , $companyname . "/" . $materialdir . "/"; /* NOTE: to overwrite the original with the new, use the same name and path for both */ /* resize original to be within the sizes specified -n $t_w and $t_h */ Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) /* make a thumbnail version */ $t_w = 100; $t_h = 120; $save = $prefix . $modelnumber . "_tn.jpg"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) } } $photo = "images/$companyname/$materialdir/$prefix$modelnumber.jpg"; $photo_rs = "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . "_rs.jpg"; $photo_tn = "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . "_tn.jpg"; Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173072 Share on other sites More sharing options...
kclark Posted February 12, 2011 Author Share Posted February 12, 2011 What if I don't want to resize the uploaded file only the thumbnail Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173298 Share on other sites More sharing options...
litebearer Posted February 12, 2011 Share Posted February 12, 2011 Simply comment the one you don't want to use, ie (BTW I noticed a typo in my code - both the resize lines need a semi-colon at the end) /* resize original to be within the sizes specified -n $t_w and $t_h */ /* Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); */ /* make a thumbnail version */ $t_w = 100; $t_h = 120; $save = $prefix . $modelnumber . "_tn.jpg"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path; Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173317 Share on other sites More sharing options...
kclark Posted February 12, 2011 Author Share Posted February 12, 2011 I am getting an error, Blank page that is. if($form == "Delete Casket") echo ""; else { /* put this at the top of your upload script */ include('resize.php'); if ($_FILES['file']['error'] > 0) { echo "Return Code: " . $_FILES['file']['error'] . "<br />"; }else{ echo "Upload: " . $_FILES['file']['name'] . "<br />"; echo "Type: " . $_FILES['file']['type'] . "<br />"; echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES['file']['tmp_name'] . "<br />"; if (file_exists("images/" . $_FILES['file']['name'])){ echo $_FILES['file']['name'] . " already exists. "; }else{ move_uploaded_file($_FILES['file']['tmp_name'], "images/$companyname/$materialdir/$prefix$modelnumber.jpg"); echo "Stored in: images/$companyname/$materialdir/$prefix$modelnumber.jpg"; /* define the resizing parameters */ /* $file is the name of the original image WITHOUT the path */ $file = $prefix . $modelnumber . "jpg"; /* $save is the name of the resized image WITHOUT the path */ $save = $prefix . $modelnumber . ".jpg"; /* $t_w is the MAXIMUM width of the new image */ $t_w = 800; /* $t_h is the MAXIMUM height of the new image */ $t_h = 600; /* $o_path is the path to the original image INCLUDE trailing slash */ $o_path = "images/" , $companyname . "/" . $materialdir . "/"; /* $s_path is the path where you want to save the new image INCLUDE trailing slash */ $o_path = "images/" , $companyname . "/" . $materialdir . "/"; /* NOTE: to overwrite the original with the new, use the same name and path for both */ /* resize original to be within the sizes specified -n $t_w and $t_h */ //Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); /* make a thumbnail version */ $t_w = 100; $t_h = 120; $save = $prefix . $modelnumber . "_tn.jpg"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); } } $photo = "images/$companyname/$materialdir/$prefix$modelnumber.jpg"; //$photo = "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . ".jpg"; $thumbnail = "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . "_th.jpg"; } Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173373 Share on other sites More sharing options...
litebearer Posted February 12, 2011 Share Posted February 12, 2011 Show us the FULL page from the absolute top to the absolute bottom Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173380 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 Enable error reporting and post the errors that are returned. You shouldn't be developing without error reporting on to begin with. Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173383 Share on other sites More sharing options...
kclark Posted February 12, 2011 Author Share Posted February 12, 2011 my page was working fine until I replaced my original upload script with that provided. My first post was my original script that worked fine. I wanted to add the thumbnail so I came here. I changed my original to what I posted the 2nd time just above. I have error reporting turned on but not getting any error, just blank page. Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173391 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 So in your php.ini file, you have the following? display_errors = On error_reporting = -1 Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173397 Share on other sites More sharing options...
kclark Posted February 12, 2011 Author Share Posted February 12, 2011 yes Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173408 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 Then something ain't working. There are parse errors in there that should be reported, namely using commas instead of periods to attempt to concatenate. You need to debug why the errors aren't being reported. Is this on a hosting server, or a local machine? Either way, create a script with just these 3 lines, and run it. Then see what values it shows for error_reporting and display_errors <?php phpinfo(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173415 Share on other sites More sharing options...
litebearer Posted February 12, 2011 Share Posted February 12, 2011 NOTE TO PIKA... being a bit un-informed, is this valid way for him to concatenate.. "images/$companyname/$materialdir/$prefix$modelnumber.jpg") OR is it better to do ... "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . ".jpg") Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173417 Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 The first should work just fine. I find that throwing in all the extra quotes and concatenation operators just leads to typos. To be absolutely safe, you can enclose the variables in curly braces too. "images/{$companyname}/{$materialdir}/{$prefix}{$modelnumber}.jpg" Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173419 Share on other sites More sharing options...
litebearer Posted February 13, 2011 Share Posted February 13, 2011 Thanks! (re-tested my resize function to be CERTAIN - works as expected) Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173424 Share on other sites More sharing options...
kclark Posted February 13, 2011 Author Share Posted February 13, 2011 The image did get upload, but no thumbnail Getting the following errors: Warning: getimagesize(images/winstonsalem/bronze/test23jpg) [function.getimagesize]: failed to open stream: No such file or directory in resize.php on line 21 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in resize.php on line 35 Warning: imagecreatefromjpeg(images/winstonsalem/bronze/test23jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in resize.php on line 39 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in resize.php on line 48 Warning: imagejpeg(): supplied argument is not a valid Image resource in resize.php on line 49 Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173477 Share on other sites More sharing options...
kclark Posted February 13, 2011 Author Share Posted February 13, 2011 I got it figured out. These lines: $o_path = "images/" . $companyname . "/" . $materialdir . "/";//line 144 /* $s_path is the path where you want to save the new image INCLUDE trailing slash */ $s_path = "images/" . $companyname . "/" . $materialdir . "/"; They both said $o_path. Now it works, thanks a bunch Quote Link to comment https://forums.phpfreaks.com/topic/227367-upload-image-for-2-files/#findComment-1173483 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.