I'm trying to resize image and upload it to FTP server without saving it on local disc but I didn't find any method how to do that. Function ftp_fput obviously doesn't TAKE image resource as argument.
I'm trying to do this:
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $width, $height, $oldWidth, $oldHeight);
ob_start();
imagejpeg($canvas);
$stream = ob_get_clean();
// ftp_->fput(imagecreatefromstring($stream));
ftp_->fput($stream);
Any help?
PHP doesn't have a function to write a string through ftp. You would have to write the file to disk and send it or you can use the php://memory or php://temp wrapper to write the data to memory and pass that to ftp_fput. There is a comment on the ftp_put manual page that has a simple function that flushes to a temp file.
http://www.php.net/manual/en/function.ftp-put.php#83260
There are a few comments on the ftp_fput manual page that cover this as well.
Related
I'm creating a png and uploading immediately to S3, I'd like to log how big that file was without having to do a seperate call to the same file on S3 to work out the size on disk.
Is this possible?
$tile = imagecreatetruecolor($tileImageSize, $tileImageSize);
imagecopy($tile, $resizedMainImage, 0, 0, $currentCoordsX, $currentCoordsY, $tileSize, $tileSize);
$writeStream = fopen("s3://bucket/file.png", 'w');
imagepng($tile, $writeStream, 9); // need filesize of this action
I've tried wrapping the imagepng in ob_start(); ob_get_length(); etc with no joy.
The thing is that you will need to save the image somewhere in order to get the size. To avoid writing to the filesystem, you can use the memory storage of php. I mean something like this:
imagepng($tile, 'php://memory/image.png');
$file_size = filesize('php://memory/image.png');
You can do this before uploading the image to the server to have the information for your log.
For my website i want to store all image using AWS S3 service using API.
How to do that thing via API/SDK
1) How to upload image/file in different folder using API (from my website).
2) How to resize/crop image on the fly. eg 50x50 px, 250x250 px.
3) Force download.
Thanks
I don't think AWS has a built-in feature to resize any stored image in S3. As eldblz mentioned, you have to do the resizing on your own. You can use S3 stream wrapper.
The S3 stream wrapper is pretty amazing: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/feature-s3-stream-wrapper.html
It will allow you to use built-in PHP functions like file_get_contents and file_put_contents.
Get details of the original file:
# If you have stream wrapper enabled,
# getimagesize will get information from the S3
# you have to pass the S3 URI though
list($width, $height) = getimagesize('s3://bucket/key');
# making the new image 50x50
$new_width = 50;
$new_height = 50;
$new_image = imagecreatetruecolor($new_width, $new_height);
Get the image data with file_get_contents(or fopen):
$data = file_get_contents('s3://bucket/key');
create an image resource from the data and resize:
$source = imagecreatefromstring($data);
# Resize
imagecopyresized($new_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Output:
header('Content-Type: image/jpeg');
imagejpeg($thumb);
Hope this helps!
Your question is a bit vague, so the answer cannot be precise. I'll give some advice or library to start:
How to upload image/file in different folder using API (from my website).
This library should be a nice place to start working with S3 and files:
https://github.com/tpyo/amazon-s3-php-class
How to resize/crop image on the fly. eg 50x50 px, 250x250 px.
This should do the trick: https://github.com/eventviva/php-image-resize
Or you can try PHP Imagick: http://php.net/manual/en/book.imagick.php
Force download.
Stackoverflow already has the answer for you:
How to force file download with PHP
Hope this helps get you started.
I have a fairly basic PHP script used to generate an image by combining several files, using variables stored in the URL, into one. For ease of explanation, here it is:
<?php
$images = array( $_GET['item1'], $_GET['item2'], $_GET['item3'] );
// Allocate new image
$img = imagecreatetruecolor(480, 480);
// Make alpha channels work
imagealphablending($img, true);
imagesavealpha($img, true);
foreach($images as $fn) {
// Load image
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
// Copy over image
imagecopy($img, $cur, 0, 0, 0, 0, 480, 480);
// Free memory
imagedestroy($cur);
}
header('Content-Type: image/png'); // Comment out this line to see PHP errors
imagepng($img);
?>
No issue with that.
I don't really understand what to do with this now though. I would like to download this to the client's downloads folder but I am unaware of and cannot find any methods for this.
I do not particularly need someone to do this for me, but just a push in the right direction for resources on doing this.
I know how to do this by temporary storing that file as a .png on my server and directing the user to a download link for that but that seems like the long way around this.
Any help please?
-Tim
You should save your code within a php file i.e. image.php. If a user calls this file passing valid arguments like this
www.example.com/image.php?item1=bild1.png&item2=bild2.png&item3=bild3.png
the file will be displayed within the browser (you don't have to save it on server side). To force a download of the image add an additional header:
header('Content-Disposition: Attachment;filename=image.png');
I think this will be an easy answer but I just can't figure it out.
I have a script that generates a pdf using fpdf and fpdi.
If I use a normal image then the pdf generates perfectly but I am trying to use an image generated dynamically by php.
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
// image
$src = $bc->getVoucher();
$pdf->Image($src, 22, 94, 100, 15);
This gives the error:
FPDF error: Image file has no extension and no type was specified: Resource id #16
If I do
$barcode = imagejpeg($src);
$pdf->Image($barcode, 22, 94, 100, 15);
presumably because the imagejpeg is actually outputting the image but the image header is set because if the jpeg header is set the image will display fine so I'm just trying to figure out the correct way of doing this.
If it's easier, I have a separate script which can generate the image, e.g. `printbarcode.php' but I don't know how to get the contents of that script into this function:
$pdf->Image($barcode, 22, 94, 100, 15);
From the error message I would guess that the $pdf->Image() function is expecting $src to be a path to a file. What you are passing seems like a object.
Try saving the image to disk and loading from there. You are almost there with imagejpeg() function - see docs on php.net. Supply a path to save the file
imagejpeg($img, 'myfile.jpg', 90); // 90 denotes high quality
$pdf->Image('myfile.jpg', 22, 94, 100, 15);
...
unlink('myfile.jpg'); // delete after
or alternatively look for a function on the fpdf library that takes image resource as a parameter.
You don't need the image handler PHP uses to manipulate the image. What you need is the actual result of the image manipulation, namely the resulting file. Use imagejpeg($img, '/path/to/some/tmp/dir/file.jpg'); and hand the path over to your $pdf->Image() call. Then after creating the PDF you can safely remove the temporarily stored image, I think.
you have to do like this with the help of php's GD library..
//Create a new image from file or URL
$img_src = imagecreatefromjpeg($src);
// Output the image
imagejpeg($img_src);
// Free up memory
imagedestroy($img_src);
Any way I can cut out a part of an existing image, and then save the result as a new image(file)?
imagecopy allows you to specify x,y,w,h arguments for the src image. Combine this with imagecreatetruecolor and you should easily be able to achieve what you want. There is even an example in the documentation for imagecopy:
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
Use imagejpeg or imagepng to save the image to a file.
You can use the gd functions for this (manual).
Load the source image (imagecreatefromstring() can be useful, so you don't have to specify the type of the image), create an output image (imagecreatetruecolor()) and use imagecopy() (if you don't want to resize it). At the end use imagepng() to output the image, or save it to a file.
Be warned that gd doesn't use image compression in memory, so your PHP process might require lots of RAM when creating a high resolution mosaic. Use imagefree() as soon as possible.