Resize and save an uploaded image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Resize and save an uploaded image

Post 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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

http://us4.php.net/manual/en/function.imagejpeg.php

imagejpeg -- Output image to browser or file
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

have fixed it by removing the header() and addng the string extension thx for the reference
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
NosajiX
Forum Newbie
Posts: 1
Joined: Thu Jul 14, 2005 9:59 am

Post by NosajiX »

the link for resolve no longer works, i too am interested in this solution, any help?
User avatar
titaniumdoughnut
Forum Commoner
Posts: 33
Joined: Wed Jul 13, 2005 2:02 pm
Location: Manhattan

Post 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);
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Post 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.
Post Reply