473,566 Members | 3,245 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 2443
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
5312
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...
0
1900
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...
1
2151
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
4
2352
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
2
2355
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...
6
4509
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...
7
2354
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...
7
17045
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
3094
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...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
926
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...

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.