note 50415 added to function.imagejpeg
| From: | nerdgirl at HATE_SPAMnerdgirl dot dk | Date: | Sun, 27 Feb 2005 20:31:18 +0000 |
| Subject: | note 50415 added to function.imagejpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85645@lists.php.net to get a copy of this message | ||
Here is an example of uploading an image, changing its size (width/height) and saving it to a new
file. It uses ftp to upload both the original and the resized image. This is usefull in situations
where ftp is your only option.
It took me a while to figure it out, so I hope this will save someone else a lot of time :-)
The following assumes that the image is uploaded from a form with a filefield called
'gfx'. I have left out all error checking to keep it simple, so remember to modify the
code, to suit your needs.
<?php
$new_width = 100;
$new_height = 200;
$tmp_image=imagecreatefromjpeg($_FILES['gfx']['tmp_name']);
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);
$new_image = imagecreatetruecolor($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
//Grab new image
ob_start();
ImageJPEG($new_image);
$image_buffer = ob_get_contents();
ob_end_clean();
ImageDestroy($new_image);
//Create temporary file and write to it
$fp = tmpfile();
fwrite($fp, $image_buffer)) {
rewind($fp);
//Upload new image
$conn_id = ftp_connect('ftp.example.tld');
ftp_login($conn_id,'user','pass');
ftp_fput($conn_id,'path/to/new/file.jpg', $fp, FTP_BINARY);
fclose($fp);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50415
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50415&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50415&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50415&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50415&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50415&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50415&report=yes
Search -- http://master.php.net/manage/user-notes.php