How to use AWS S3 for image processing (PHP) - php

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.

Related

Image resize before user-side

UPDATE:
now I learned more I looked alot and modified I got this:
function show_image($image, $new_width, $new_height) {
//$this->helper('file'); why need this?
//$image_content = read_file($image); We does not want to use this as output.
list($old_width,$old_height) = getimagesize("$image");
//resize image
$image = imagecreatefromjpeg($image);
$thumbImage = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($thumbImage, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
imagedestroy($image);
//imagedestroy($thumbImage); do not destroy before display :)
ob_end_clean(); // clean the output buffer ... if turned on.
header('Content-Type: image/jpeg');
imagejpeg($thumbImage); //you does not want to save.. just display
imagedestroy($thumbImage); //but not needed, cause the script exit in next line and free the used memory
exit;
}
when using this function I get a file, but I need to get a URL
I wanna be able to use:
<img src=" ... ">
Since you can't use GD on the server, a good option might be to handle it on the client side.
One idea that comes to mind is loading the image into an html5 canvas, and using that to manipulate the image characteristics before uploading it to the server.
This post talks about how you'd go about uploading image data straight from a canvas and saving it as an image on your server.
How to save a HTML5 Canvas as an image on a server
You also will likely need to take a look at the JavaScript File API to load the image into the canvas on the client side.
It's not necessarily an easy solution, but it might be your best shot without access to GD and stuff.

Create thumbnails using PHP or manully add thumbnails?

I have a Folder called "Gallery" inside which i have created a folder for Thumbails called "Gallery-Thumbs", where i manually add thumbnails.
My question is whether it is better to manually add thumbnails or create thumbnails dynamically using "GD" or "ImageMagick".
Problems with adding Thumbnails manually
If i forget to add a thumbnail for photo the chain of the Gallery breaks
It is tedious to resize all the images for creating the thumbnails
So does using PHP ImageProcessing function add additional overhead in creating thumbnails or is the correct approach?
-- Updated for answering Queries
How you add images to the "gallery" folder?
Through Direct file upload
How those images and thumbnails are accessed?
I use glob to get list of files in respective folders
How do you (want to) map images to thumbnails?
For thumbnails i use imageName-thumb.jpg,
so they appear in the list in the same order as they are in main image folder
Use PHP to create thumbnails once and save them in a "thumbnails" directory with the same file names. Then use these now-ready-to-be-used thumbnails directly.
If your going with some Dynamic image processing tool then it will be little bit time consuming process, except that it will be a great technique.
If you are going with the manual process then:
Make thumbnail compulsory using some client side script
It depends on your image orientation
I create the thumbnails during upload and either save in a different directory with the same name or add th_ to the front of the filename.
You could also upload some code to generate all the thumbs again for the images you currently have in your gallery in case you have missed any.
You could also resize to different sizes at the same time, watermark or add other effects.
This is my gallery and I have a thumb and normal size; the thumb was sharpened on upload and the normal watermarked. Bother versions have a drop shadow and the resizing etc. was done with Imagemagick.
http://www.rubble.info/gallery/
If you check the file is there before displaying it and it is missing you can show a default image.
You can check out my website for lots of different things you can do with php and Imagemagick.
I highly recommend using GD with some sort of caching to keep the ones that don't change. There is, however, already a superb library for doing this. It's an absolute favourite of mine and gives you easy image compression and resizing just with a GET URL.
Try Smart Image Resizer by ShiftingPixel:
http://shiftingpixel.com/2008/03/03/smart-image-resizer/
You can, if you wish, use a website I made as a reference using its page source: http://www.eastwood-whelpton.co.uk/about/gallery.php
Nearly all images on this website use the Smart Image Resizer GD library.
I can also provide the PHP code I used if you wish for examples. This particular code automatically adds any images found in my gallery folder to this page.
As stated previously you need PHP 5 with GD support. If you have these then here is a very handy function to create thumbnails of a given dimensions and quality ($options) from every image in a given directory ($from_dir) and save them to another directory ($to_dir).
function make_thumbnails($from_dir,$to_dir, $options){
$files = scandir($from_dir);
$exclude = array('.','..','etc.');
foreach($files as $fi => $fv){
if(!in_array($fv,$exclude)){
$from_file = $from_dir.$fv;
$to_file = $target_dir.$fv;
list($img_width, $img_height,$img_type) = getimagesize($from_file);
$scale = min($options['max_width'] / $img_width,
$options['max_height'] / $img_height
);
$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
$new_img = imagecreatetruecolor($new_width, $new_height);
$src_img = imagecreatefromjpeg($from_file);
$success = $src_img && imagecopyresampled(
$new_img,
$src_img,
0, 0, 0, 0,
$new_width,
$new_height,
$img_width,
$img_height
) && imagejpeg($new_img, $to_file, $options['quality']);
//Monitor results with $success - returns 1 or null
echo '<br />success:['.$success.']';
}
}
}
//Set options
$from_dir = ':/source/dir';
$to_dir = ':/destination/dir';
$options = array();
$options['max_width'] = 100;
$options['max_height'] = 100;
$options['quality'] = 100;
// Make thumbs...
make_thumbnails($from_dir,$to_dir,$options);

Image uploading concept in PHP

how to take image from one folder and rename and re-size the images and move to another folder?
I need to get image from one folder and rename and re-size the same image and move into another folder using php
You will most likely be using gd for resizing the images.
Here is a pretty crappy, but hopefully useful code sample. In this case, $originalName is the name given in the $_FILES array's tmp_name position. I am resizing to a width of 1200 in this case, with the height adapting according to such width. You might (and most likely will) not desire this behavior. This is just some ugly code I used in some courses I taught about 3 years ago, I don't have the newer samples in this computer so you will have to get used to the idea :)
$newDir is where the file will be located. by calling imagejpeg or imagepng and passing the filename as second argument, it means to the function that you wish to save the image in that location.
if ($type == 'image/jpeg') {
$original = imagecreatefromjpeg($originalName);
}
else {
$original = imagecreatefrompng($originalName);
}
$width = imagesx($original);
$height = imagesy($original);
//prepare for creation of image with width of 1000
$new_height = floor($height * (1200 / $width));
// create the 1200 width image
$tmp_img = imagecreatetruecolor(1200, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $original, 0, 0, 0, 0,
1200, $new_height, $width, $height);
//create a random and unique name to identify (here it isn't that random ;)
$newDir = '/this/is/some/directory/and/filename.';
if ($type == 'image/jpeg') {
imagejpeg($tmp_img, $newDir."jpg");
}
else {
imagepng($tmp_img, $newDir."png");
}
Many file system functions are already built-in with PHP (e.g. rename), and you'll find most of what you need to resize images by using the GD library here.
There are libraries available in PHP for image resize.
Here are some useful links you may like.
http://www.fliquidstudios.com/2009/05/07/resizing-images-in-php-with-gd-and-imagick/
http://php.net/manual/en/book.image.php
PHP/GD - Cropping and Resizing Images
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
Use imagick http://php.net/manual/en/imagick.resizeimage.php
If I were you I would write a PE using a diffent language (one that you might be best used to) to adjust anything of the given image then just feel free to phpexec it to do all the steps you mentioned, you can sit relax and wait for the end result. HHAHAHHA :-)
Use imagick library to resize; it's good.

Resizing image with PHP

I used a very simple code to resize image with PHP; but surprisingly it does not work for some images. The problem should be associated with imagecreatefromjpeg(), as it will generate a black image (which is of the background image).
$picture="test5.jpg";
$url="http://www.pokerpurist.com/uploadedImages/bettingpro/NewsImages/TN98553_Perla-Beltran.jpg";
list($width, $height) = getimagesize($url);
$new_height = $height / $width * 400;
$image_p = imagecreatetruecolor(400, $new_height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height);
imagejpeg($image_p, $picture);
echo "<img src='$picture' />";
This problem happens offen, and I included an example image. What is the problem with these images leading to this problem? It seems to be a normal JPG image.
By the way, is it the simplest and most efficient way to resize image with PHP/GD2?
Your example image is a PNG, not JPEG. You probably need to put some detection code in place...
Edit: exif-imagetype or ImageMagick might be of some use.
#By the way, is it the simplest and most efficient way to resize image with PHP/GD2?
Use Asido: PHP Image Processing Solution
Asido supports the following features:
pluggable drivers for GD2 (php_gd2), MagickWand (php_magickwand),
ImageMagick extension (php_imagick) as well as ImageMagick shell
commands
"hack" drivers: workarounds for certain disabilities of a particular driver by using some of the other functionality provided
by the environment
various resize functionality: proportional resize, resize only by width or height, stretch resize, fit resize, frame resize
watermark images, including tiling watermark and automatic scaling of large watermarks
rotate images
copy images onto one another
crop images
grayscale images
convert images between different filetypes
If you can't access Asido website, you can download Asido from SourceForge.net

dynamic image resize using php

I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.

Categories