This is a discussion on image upload and resize question within the PHP Language forums, part of the PHP Programming Forums category; I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a "file upload form" that works OK, but I have been unsuccessful
in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save a thumbnail.jpg .... all at the same time. Links to a working example would be appreciated. Thanks. |
|
|||
|
could you show the html form, and the php code that deals with the file
to save it and tries to resize it? DH wrote: > I have a "file upload form" that works OK, but I have been unsuccessful > in my attempt to also resize the uploaded .JPG (if it is too wide), > over-writing the original .JPG, and then create and save a thumbnail.jpg > ... all at the same time. Links to a working example would be > appreciated. Thanks. |
|
|||
|
I noticed that Message-ID: <7cGdnVcyXcwmHJTfRVn-vQ@comcast.com> from DH
contained the following: >I have a "file upload form" that works OK, but I have been unsuccessful >in my attempt to also resize the uploaded .JPG (if it is too wide), >over-writing the original .JPG, and then create and save a thumbnail.jpg >... all at the same time. Links to a working example would be >appreciated. I've got some code that needs tidying up, but you are welcome to have a look. $thumbpath = "images/thumb/"; $mainpath="images/main/"; $max_size = 1000000; if (!isset($_FILES['userfile'])) exit; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { if ($_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; } if($_FILES['userfile']['type']=="image/gif"){ $createfunction="imagecreatefromgif";} else{$createfunction="imagecreatefromjpeg";} if (($_FILES['userfile']['type']=="image/gif") || ($_FILES['userfile']['type'])=="image/pjpeg" || ($_FILES['userfile']['type']=="image/jpeg")) { //check if file already uploaded //if (file_exists($thumbpath ."s_". $_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; } //make a copy of original file //$res = copy($_FILES['userfile']['tmp_name'], $path . //$_FILES['userfile']['name']); //create thumbnail image $src_img = $createfunction($_FILES['userfile']['tmp_name']); $new_w = 120; $new_h = imagesy($src_img)/(imagesx($src_img)/$new_w); $dst_img = imagecreatetruecolor($new_w,$new_h); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img)); imagejpeg($dst_img, $thumbpath ."s_". $_FILES['userfile']['name'], 100); //create big image if(imagesx($src_img)>450){ $new_w = 450; $new_h = imagesy($src_img)/(imagesx($src_img)/$new_w); $dst_img = imagecreatetruecolor($new_w,$new_h); $res=imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img)); imagejpeg($dst_img, $path ."b_". $_FILES['userfile']['name'], 50); $notify = "<span class=\"indent10bold\">Main image resampled</span>"; } else{ $res = copy($_FILES['userfile']['tmp_name'], $mainpath .."b_".$_FILES['userfile']['name']); $notify = "<span class=\"indent10bold\">Main image not resampled</span>"; } if (!$res) { echo "<span class=\"indent10bold\">Upload failed!</span><br>\n"; exit; } else { echo "<span class=\"indent10bold\">Upload sucessful!</span><br><br>\n"; } $filename=$_FILES['userfile']['name']; printf("<span class=\"indent10bold\">Main : </span>%sb_%s<br>\n",$path,$filename); printf("<span class=\"indent10bold\">Thumbnail : </span>%ss_%s<br>\n",$thumbpath,$filename); //echo "<span class=\"indent10bold\">Main File Name: </span>".$_FILES['userfile']['name']."<br>\n"; echo "<span class=\"indent10bold\">Original File Size: </span> ".$_FILES['userfile']['size']." bytes<br>\n"; echo "<span class=\"indent10bold\">File Type: </span>".$_FILES['userfile']['type']."<br>\n"; echo "$notify.<br>\n"; } else { echo "<span class=\"indent10bold\">Please use a .jpg or a .gif file</span><br>\n"; exit; } //printf("<img src=\"%s%s\">",$path,$filename); //printf("<img src=\"%sb_%s\"alt=\"%s\">",$path,$filename,$mainur l_alt); printf("<span class=\"indent10bold\">Image Thumbnail: </span><br><br><span class=\"indent10bold\"><img src=\"%ss_%s\"alt=\"%s\"></span><br>",$thumbpath,$filename,$thumburl_alt); imagedestroy($src_img); imagedestroy($dst_img); } //end of image upload -- Geoff Berrow (put thecat out to email) It's only Usenet, no one dies. My opinions, not the committee's, mine. Simple RFDs http://www.ckdog.co.uk/rfdmaker/ |
|
|||
|
I noticed that Message-ID: <30fj01tc9o4anljstjhlnn8sr914bat8vu@4ax.com>
from Geoff Berrow contained the following: >I've got some code that needs tidying up, but you are welcome to have a >look. For instance s/$path/$mainpath -- Geoff Berrow (put thecat out to email) It's only Usenet, no one dies. My opinions, not the committee's, mine. Simple RFDs http://www.ckdog.co.uk/rfdmaker/ |
|
|||
|
Geoff Berrow wrote:
> I noticed that Message-ID: <7cGdnVcyXcwmHJTfRVn-vQ@comcast.com> from DH > contained the following: > > >>I have a "file upload form" that works OK, but I have been unsuccessful >>in my attempt to also resize the uploaded .JPG (if it is too wide), >>over-writing the original .JPG, and then create and save a thumbnail.jpg >>... all at the same time. Links to a working example would be >>appreciated. > > > I've got some code that needs tidying up, but you are welcome to have a > look. > > > > $thumbpath = "images/thumb/"; > $mainpath="images/main/"; > $max_size = 1000000; > > if (!isset($_FILES['userfile'])) exit; > > > if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { > > if ($_FILES['userfile']['size']>$max_size) > { echo "The file is too big<br>\n"; exit; } > > if($_FILES['userfile']['type']=="image/gif"){ > $createfunction="imagecreatefromgif";} > else{$createfunction="imagecreatefromjpeg";} > > if (($_FILES['userfile']['type']=="image/gif") || > ($_FILES['userfile']['type'])=="image/pjpeg" || > ($_FILES['userfile']['type']=="image/jpeg")) { > > //check if file already uploaded > //if (file_exists($thumbpath ."s_". $_FILES['userfile']['name'])) { echo > "The file already exists<br>\n"; exit; } > > //make a copy of original file > //$res = copy($_FILES['userfile']['tmp_name'], $path . > //$_FILES['userfile']['name']); > > //create thumbnail image > $src_img = $createfunction($_FILES['userfile']['tmp_name']); > $new_w = 120; > $new_h = imagesy($src_img)/(imagesx($src_img)/$new_w); > $dst_img = imagecreatetruecolor($new_w,$new_h); > imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, > imagesx($src_img), imagesy($src_img)); > imagejpeg($dst_img, $thumbpath ."s_". > $_FILES['userfile']['name'], 100); > > //create big image > if(imagesx($src_img)>450){ > $new_w = 450; > $new_h = imagesy($src_img)/(imagesx($src_img)/$new_w); > $dst_img = imagecreatetruecolor($new_w,$new_h); > $res=imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, > $new_h, imagesx($src_img), imagesy($src_img)); > imagejpeg($dst_img, $path ."b_". > $_FILES['userfile']['name'], 50); > $notify = "<span class=\"indent10bold\">Main image resampled</span>"; > } > else{ > $res = copy($_FILES['userfile']['tmp_name'], $mainpath > .."b_".$_FILES['userfile']['name']); > $notify = "<span class=\"indent10bold\">Main image not > resampled</span>"; > } > > if (!$res) { echo "<span class=\"indent10bold\">Upload > failed!</span><br>\n"; exit; } else { echo "<span > class=\"indent10bold\">Upload sucessful!</span><br><br>\n"; > > } > > $filename=$_FILES['userfile']['name']; > printf("<span class=\"indent10bold\">Main : > </span>%sb_%s<br>\n",$path,$filename); > printf("<span class=\"indent10bold\">Thumbnail : > </span>%ss_%s<br>\n",$thumbpath,$filename); > //echo "<span class=\"indent10bold\">Main File Name: > </span>".$_FILES['userfile']['name']."<br>\n"; > echo "<span class=\"indent10bold\">Original File Size: </span> > ".$_FILES['userfile']['size']." bytes<br>\n"; > echo "<span class=\"indent10bold\">File Type: > </span>".$_FILES['userfile']['type']."<br>\n"; > echo "$notify.<br>\n"; > } else { echo "<span class=\"indent10bold\">Please use a .jpg or a .gif file</span><br>\n"; exit; } > > //printf("<img src=\"%s%s\">",$path,$filename); > //printf("<img src=\"%sb_%s\"alt=\"%s\">",$path,$filename,$mainur l_alt); > printf("<span class=\"indent10bold\">Image Thumbnail: > </span><br><br><span class=\"indent10bold\"><img > src=\"%ss_%s\"alt=\"%s\"></span><br>",$thumbpath,$filename,$thumburl_alt); > imagedestroy($src_img); > imagedestroy($dst_img); > } > //end of image upload > The code that I've come up with now, by combining an upload script from http://www.phpfreaks.com/quickcode/code/272.php along with Geoff Berrow's input is rather lengthy and is posted here: http://phpvs.com/misc/imgupld.php The script needs additional work, in terms of deleting the original image after sucessfully creating a properly sized image and thumbnail. Also exploding the filename on '.' and appending _new and _thumb (instead of prepending 'new_' and 'thumb_'). |