Page 1 of 1
Resize and save an uploaded image
Posted: Sun Jun 19, 2005 9:56 am
by andylyon87
Hey guys I am having a problem with images. I want to be able to upload an image and have it resized to a more manageable size (which I have done) but want it to then be saved to the server. How would I go about doing this with the code underneath. Once I have found out how to do this I can change it to suit my needs. this code is straight from
http://www.php.net but how would I save $thumb:
Code: Select all
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreate($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
Thanks guys
Posted: Sun Jun 19, 2005 10:07 am
by andylyon87
this code prints an image of test.jpg at 50% size, however I would like to now be able to save this new image to the server.
Posted: Sun Jun 19, 2005 10:11 am
by Todd_Z
Posted: Sun Jun 19, 2005 10:44 am
by andylyon87
Im sorry this is the first time I have tried this and just cannot see how I can save the created image to a folder.
Have looked at three different scripts and as I am not familiar with any of the commands used havng never done this before cannot situate the one that writes the image to the folder.
If you can please explain further.
andy
Posted: Sun Jun 19, 2005 10:47 am
by andylyon87
have fixed it by removing the header() and addng the string extension thx for the reference
Posted: Sun Jun 19, 2005 10:57 am
by Todd_Z
You only need the header if you are going to output the resulting or original image in the browser, sometimes this is a nice touch though, for example showing them what their thumbnail looks like. But otherwise, if its just to save the thumbnail, no need for headers and all that junk.
Posted: Thu Jul 14, 2005 11:49 am
by NosajiX
the link for resolve no longer works, i too am interested in this solution, any help?
Posted: Thu Jul 14, 2005 2:12 pm
by titaniumdoughnut
I've found that permissions on the folder you're trying to save to, among other things, can screw this up. I use an FTP connection to save the file.
($newimage should contain the image to save)
Code: Select all
// connect to ftp
if(!($ftp = ftp_connect("localhost")))
{
exit();
}
// log in to ftp
if(!ftp_login($ftp, "username", "password"))
{
exit();
}
// make sure passive mode is off
ftp_pasv($ftp, FALSE);
// change directory
if(!(ftp_chdir($ftp, "/www/foldername/")))
{
exit();
}
//Upload new image
$newname = "test.jpg";
ftp_fput($ftp, $newname, $newimage, FTP_BINARY);
fclose($fp);
Posted: Wed Jul 20, 2005 6:54 pm
by tpra21
Not sure exactly what is wanted here, but I would save the path of the file to a MySQL db, and then copy the file (picture) to a folder named "uploads."
Example, save the path 1_01.jpg in the db, and then copy that same name into the folder to represent your uploaded picture. Then, when you want to display the image, pull the path from the db (1_01.jpg) and place it in a <img> tag, ex:<img src="uploads/1_01.jpg>. Then the <img> tag will refrence the picture named 1_01.jpg that you saved in your "uploads" folder.