note 50415 deleted from function.imagejpeg by cmb
| From: | cmb@php.net | Date: | Tue, 04 Oct 2016 15:19:18 +0000 |
| Subject: | note 50415 deleted from function.imagejpeg by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206750@lists.php.net to get a copy of this message | ||
Note Submitter: nerdgirl at HATE_SPAMnerdgirl dot dk
----
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);
?>