473,699 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

image upload and resize question

DH
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.
Jul 17 '05 #1
4 2453
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.


Jul 17 '05 #2
I noticed that Message-ID: <7c************ ********@comcas t.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="imag es/main/";
$max_size = 1000000;

if (!isset($_FILES['userfile'])) exit;
if (is_uploaded_fi le($_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 ="imagecreatefr omgif";}
else{$createfun ction="imagecre atefromjpeg";}

if (($_FILES['userfile']['type']=="image/gif") ||
($_FILES['userfile']['type'])=="image/pjpeg" ||
($_FILES['userfile']['type']=="image/jpeg")) {

//check if file already uploaded
//if (file_exists($t humbpath ."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_im g)/(imagesx($src_i mg)/$new_w);
$dst_img = imagecreatetrue color($new_w,$n ew_h);
imagecopyresamp led($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h,
imagesx($src_im g), imagesy($src_im g));
imagejpeg($dst_ img, $thumbpath ."s_".
$_FILES['userfile']['name'], 100);

//create big image
if(imagesx($src _img)>450){
$new_w = 450;
$new_h = imagesy($src_im g)/(imagesx($src_i mg)/$new_w);
$dst_img = imagecreatetrue color($new_w,$n ew_h);
$res=imagecopyr esampled($dst_i mg, $src_img, 0, 0, 0, 0, $new_w,
$new_h, imagesx($src_im g), imagesy($src_im g));
imagejpeg($dst_ img, $path ."b_".
$_FILES['userfile']['name'], 50);
$notify = "<span class=\"indent1 0bold\">Main image resampled</span>";
}
else{
$res = copy($_FILES['userfile']['tmp_name'], $mainpath
.."b_".$_FILE S['userfile']['name']);
$notify = "<span class=\"indent1 0bold\">Main image not
resampled</span>";
}

if (!$res) { echo "<span class=\"indent1 0bold\">Upload
failed!</span><br>\n"; exit; } else { echo "<span
class=\"indent1 0bold\">Upload sucessful!</span><br><br>\n ";

}

$filename=$_FIL ES['userfile']['name'];
printf("<span class=\"indent1 0bold\">Main :
</span>%sb_%s<br> \n",$path,$file name);
printf("<span class=\"indent1 0bold\">Thumbna il :
</span>%ss_%s<br> \n",$thumbpath, $filename);
//echo "<span class=\"indent1 0bold\">Main File Name:
</span>".$_FILES['userfile']['name']."<br>\n";
echo "<span class=\"indent1 0bold\">Origina l File Size: </span>
".$_FILES['userfile']['size']." bytes<br>\n";
echo "<span class=\"indent1 0bold\">File Type:
</span>".$_FILES['userfile']['type']."<br>\n";
echo "$notify.<br>\n ";
} else { echo "<span class=\"indent1 0bold\">Please use a .jpg or a .gif file</span><br>\n"; exit; }

//printf("<img src=\"%s%s\">", $path,$filename );
//printf("<img src=\"%sb_%s\"a lt=\"%s\">",$pa th,$filename,$m ainurl_alt);
printf("<span class=\"indent1 0bold\">Image Thumbnail:
</span><br><br><s pan class=\"indent1 0bold\"><img
src=\"%ss_%s\"a lt=\"%s\"></span><br>",$thu mbpath,$filenam e,$thumburl_alt );
imagedestroy($s rc_img);
imagedestroy($d st_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/
Jul 17 '05 #3
I noticed that Message-ID: <30************ *************** *****@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/
Jul 17 '05 #4
DH
Geoff Berrow wrote:
I noticed that Message-ID: <7c************ ********@comcas t.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="imag es/main/";
$max_size = 1000000;

if (!isset($_FILES['userfile'])) exit;
if (is_uploaded_fi le($_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 ="imagecreatefr omgif";}
else{$createfun ction="imagecre atefromjpeg";}

if (($_FILES['userfile']['type']=="image/gif") ||
($_FILES['userfile']['type'])=="image/pjpeg" ||
($_FILES['userfile']['type']=="image/jpeg")) {

//check if file already uploaded
//if (file_exists($t humbpath ."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_im g)/(imagesx($src_i mg)/$new_w);
$dst_img = imagecreatetrue color($new_w,$n ew_h);
imagecopyresamp led($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h,
imagesx($src_im g), imagesy($src_im g));
imagejpeg($dst_ img, $thumbpath ."s_".
$_FILES['userfile']['name'], 100);

//create big image
if(imagesx($src _img)>450){
$new_w = 450;
$new_h = imagesy($src_im g)/(imagesx($src_i mg)/$new_w);
$dst_img = imagecreatetrue color($new_w,$n ew_h);
$res=imagecopyr esampled($dst_i mg, $src_img, 0, 0, 0, 0, $new_w,
$new_h, imagesx($src_im g), imagesy($src_im g));
imagejpeg($dst_ img, $path ."b_".
$_FILES['userfile']['name'], 50);
$notify = "<span class=\"indent1 0bold\">Main image resampled</span>";
}
else{
$res = copy($_FILES['userfile']['tmp_name'], $mainpath
.."b_".$_FILE S['userfile']['name']);
$notify = "<span class=\"indent1 0bold\">Main image not
resampled</span>";
}

if (!$res) { echo "<span class=\"indent1 0bold\">Upload
failed!</span><br>\n"; exit; } else { echo "<span
class=\"indent1 0bold\">Upload sucessful!</span><br><br>\n ";

}

$filename=$_FIL ES['userfile']['name'];
printf("<span class=\"indent1 0bold\">Main :
</span>%sb_%s<br> \n",$path,$file name);
printf("<span class=\"indent1 0bold\">Thumbna il :
</span>%ss_%s<br> \n",$thumbpath, $filename);
//echo "<span class=\"indent1 0bold\">Main File Name:
</span>".$_FILES['userfile']['name']."<br>\n";
echo "<span class=\"indent1 0bold\">Origina l File Size: </span>
".$_FILES['userfile']['size']." bytes<br>\n";
echo "<span class=\"indent1 0bold\">File Type:
</span>".$_FILES['userfile']['type']."<br>\n";
echo "$notify.<br>\n ";
} else { echo "<span class=\"indent1 0bold\">Please use a .jpg or a .gif file</span><br>\n"; exit; }

//printf("<img src=\"%s%s\">", $path,$filename );
//printf("<img src=\"%sb_%s\"a lt=\"%s\">",$pa th,$filename,$m ainurl_alt);
printf("<span class=\"indent1 0bold\">Image Thumbnail:
</span><br><br><s pan class=\"indent1 0bold\"><img
src=\"%ss_%s\"a lt=\"%s\"></span><br>",$thu mbpath,$filenam e,$thumburl_alt );
imagedestroy($s rc_img);
imagedestroy($d st_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_').

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
5341
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
0
1910
by: Mattia | last post by:
************************************************** Manage image without exhausted memory ************************************************** Hi; I have a big problem. I must create a script that upload an image an then resize it, if width or height are more than 250px. Now, after upload an image (in this example I suppose that it's a JPEG image):
1
2158
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
4
2357
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
2
2364
by: Poppa Pimp | last post by:
ImageResizer.php Image Resizer PLEASE HELP The URL of the page this is on in my site is http://poppa-pimps-wallpapers.com//ImageResizer.php You can click on browse and get image,but when you upload image it will go to another page and says ]((unable to create emp directory)) Here is a site to be able to see script actually work http://tech.tailoredweb.com/image-editor-52/ and can be DL from there also. I am using FP 2003 and...
6
4525
by: Bob Bedford | last post by:
Hi all, I've a classic upload form where people may select images on their computer to send on my website. Since this is done by people not very confortable with computers, some sent pictures are very huge (3MB) and I do resize them anyway since I only allow 500px wide pictures. Instead of letting people upload the 3MB file then resize it on the server,
7
2363
by: xx75vulcan | last post by:
Hi, I've got a PHP Upload Form that works great, unless that is, the image your uploading has been modified through a photo editing software. Example: if I upload the image straight from a camera or other, it uploads fine. However, If I want to rotate the image, or resize it with photoshop or simmilar, making sure to save it again as a jpg, the PHP upload won't upload the image.
7
17054
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir = "photo/thumbs/"; // Path To Thumbnails Directory $twidth = "125"; // Maximum Width For Thumbnail Images
3
3102
by: neovantage | last post by:
Hey all, i have created image by mixing logo and the original image. It create image and show in the same window. I have done 3 steps for this. First i upload an image to my server by using move_upload_file, then i resize it using class "thumbnail.class.php" My next step was to embed company logo in it(which i already done) and save new embedded logo image to the path where i first upload the original image i mean i want to overwrite...
0
8617
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7751
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.