Image manipulation using PHP: force 1:1 aspect ratio (sqaure images) - php

I am looking to add a background to images that users upload that are not square. So if they upload a tall and skinny photo I want to add a white background to the sides of the image to make the resulting image have an aspect ratio of 1:1. Is this possible using PHP or javascript?

You can use the GD library for what, with a library called Wideimage it's a breeze:
$image = WideImage::load('img_form_field_name');
$size = max($image->getHeight(), $image->getWidth());
$white = $image->allocateColor(255, 255, 255);
$image->resizeCanvas($size, $size, 'center', 'center', $white);
See the documentation and examples, many functions can even be tested interactively.

The GD library is the most commonly used image manipulation package. It's a set of functions often installed with PHP which handle image manipulation.
What you'll want to do is either scale and crop your image to a specific aspect ratio so that you place your image on a square canvas and cut off whatever does fit or
You'll want to simply resize your image to a fixed aspect ratio and place it on a square canvas with whitespace around it.
Either way, this tutorial should point you in the right direction
http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/

Yep you'll want to look into either the GD library or ImageMagik. There are plenty of tutorials available for this task.
Functions like imagecreatetruecolor() etc will allow you to create a new image, and then stack the uploaded image on top of it and save it as a new file.

Yes.
http://www.php.net/manual/en/refs.utilspec.image.php

Related

Resize a PHP GD-generated image in PHP and display it

I am making an avatar script from scratch and am having some problems. I got transparency working, and multi-image support for heads, bodies, shirts, etc.
Anyhow, I want to be able to generate specific sizes of the avatar within the PHP script. At this time, I have the variable $baseImage, which is an image generated using the GD script below:
$baseImage = imagecreatefrompng($startAsset);
imagealphablending($baseImage, true);
imagesavealpha($baseImage, true);
... combine all images into $base here
header("Content-type: image/png");
imagepng($baseImage);
The size of the image this generates is 350x550 (pixels) and I want to be able to get a smaller size.
I've done research but cannot find a working solution. What built-in PHP GD functions can resize this, retain transparency, and keep the great quality/colors?
There is no way to change the size of an image resource directly. Instead, you need to create a new image of the desired size and use imagecopyresampled to copy from the fullsize image to the resized one.

PHP Imagick - convert image to greyscale (very bad result)

I was doing some image editing with PHP, since GD provides less functionalities, I switched to Imagick.
One of the processes is to greyscale images. Everything went fine (locally on Windows 7, Imagick 2.2.1-dev 6.5.8-7 Q16) till I uploaded the script to my web hosting server (Linux, Imagick 3.0.1, 6.2.8, 2010-10-20, Q16).
I'v tried to change the quality, but it didn't improve anything.
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(100);
Here is the results from GD, Imagick and Photoshop
I believe something's wrong with version 3.0.1. Can someone please confirm that?
Q1: Is there an alternative way to convert an image to greyscale with Imagick?
Q2: Is it possible to convert a GD resource to Imagick? So I can use imagefilter($img, IMG_FILTER_GRAYSCALE); to get the correct result and then output with Imagick.
ps: For Q2, you might suggest me to just use GD to process the image. But the problem is that imagejpeg() cannot save images with resolution preserved. and that is actually the reason I switched to Imagick.
This is my preferred way to make a B&W photo in php/imagick: $im = $im->fxImage('intensity');
That applies a function to the image, where intensity is equal to 0.299*red+0.587*green+0.114*blue.
That formula is based on how our eyes are more sensitive to different colours, and as such the difference between that and a "flat" grayscale image really is night and day.
More details here:
http://php.net/manual/en/imagick.fximage.php
http://www.imagemagick.org/script/fx.php
function ImagickToGD($imagick){
$tmpfile = tmpfile();
$imagick->writeImage($tmpfile);
return imagecreatefromstring(file_get_contents($tmpfile));
}
Note that this function does not do any cleanup (except the temp file, which PHP cleans automatically).
So, for example, your code should look like:
$img = new Imagick();
// ...
$gd = ImagickToGD($img);
unset($img); // destroy imagick
imagefilter($gd, IMG_FILTER_GRAYSCALE);
imagejpeg($gd, $target_name, 100);
imagedestroy($gd);
Also, I did not understand the part about "preserving resolution". There is nothing in these operations relating to resolution. My guess is you meant compression? If you want full quality (ie, no compression) simply use 100 as compression value (as I did).
This results in maintaining the existing quality, since opening an image of 70% quality and saving it back with 70% quality actually decreases the final quality by 49% (70% of 70%).
function GDToImagickTo($gd){
$tmpfile = tmpfile();
imagepng($tmpfile); // Png is our best image deal:
// lossless compression, transparency etc..
$imagick = new Imagick()
$imagick->readImage($tmpfile);
return $imagick;
}
Refer this website and check out the image Magick operators found here www.rubblewebs.co.uk/imagemagick/
Also go with www.fmwconcepts.com/imagemagick/ you will find some examples out here...
You can use the image class what you prefer and then use the method readImageBlob to send it to the imagick http://www.php.net/manual/en/imagick.readimageblob.php

php image manipulation - fade to transparency

does anyone know how to apply fade effect to an image using PHP ? what I am looking for is a way to apply gradient transparency ( i mean : at the top , the image is opaque , which gradually gets more and more transparent , and at the bottom it is completely transparent).
i have been reading up on http://php.net/manual/en/function.imagecolortransparent.php , but did not see anything about applying a gradient effect to an image.
i also read : PHP - Generate transparency (or opacity) gradient using image , but it kinda trailed off without any solution!
I am also open to any other suggestion / libraries that can do this from command line.
Obviously you'll need to work with a png for this effect, but you can convert any png into a jpg using php. The following question I believe covers what you are asking about. Part of the code will have to be removed to clear the image reflection effect.
Can You Get a Transparent Gradient using PHP ImageMagick?
The piece of code which seems to do what you are trying to accomplish is:
$im = new Imagick('image.jpg'); //Reference image location
if (!$im->getImageAlphaChannel()) {
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET);
}
$refl = $im->clone();
$refl->flipImage();
$gradient = new Imagick();
$gradient->newPseudoImage($refl->getImageWidth() + 10, $refl->getImageHeight() + 10, "gradient:transparent-black");

kohana 3.0 resize image before uploading?

I want to upload some images to the server, but first of all i want them croped, and resized to some certain dimensions.
Now i am doing the simple upload and save like that:
$header_image = Upload::save($_FILES['sale_picture_header'],NULL,APPPATH.'media'.'/');
$image_header = Model::factory('image');
$image_header->name = basename($header_image);
$image_header->save();
(excluding the validation).
How can i crop or resize the image to some desired dimensions, in Kohana 3.0? I couldn't find any relevant documentation regarding that.
Thank you!
Did you try with the image package:
// Resize to 200 pixels on the shortest side
$image->resize(200, 200);
// Resize to 200x200 pixels, keeping aspect ratio
$image->resize(200, 200, Image::INVERSE);
// Resize to 500 pixel width, keeping aspect ratio
$image->resize(500, NULL);
// Resize to 500 pixel height, keeping aspect ratio
$image->resize(NULL, 500);
// Resize to 200x500 pixels, ignoring aspect ratio
$image->resize(200, 500, Image::NONE);
source:
http://kohanaframework.org/3.0/guide/api/Image#resize
You can use the ImageMagick library, or the GD library for image manipulation.
Also note that these are strictly server-side, and will not happen before upload. For that you'd need some client-side plugin capable of image manipulation such as Flash or Java.
I found something very interesting. The question was, how to perform this action, "resize then crop" an image to fit perfectly a determined box, and this is the solution:
Image::factory($file)
->resize(128, 149, Image::PRECISE)
->crop(128, 149)
->save(DOCROOT.$filename);
Note that you must use Image::PRECISE, instead of IMAGE::AUTO.
Hope this helps someone.

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