Imagick: open file from path, resize, save to folder - php

I'm trying to append several images together into one big image, but the first hurdle is actually just getting the image to open.
I am attempting to do the following:
Open image from path
Size it with the specified dimensions
Save it to the current folder
I have the following:
define('WIDTH', 600);
define('HEIGHT', 800);
$img = new Imagick();
$img->readImage('dress.jpg');
$img->writeImage('image1.png');
I can get the image to save, but I do not know where to add the WIDTH and HEIGHT definitions?
I tried doing the following:
$img->newImage(WIDTH, HEIGHT, $img);

$img = new Imagick();
$img->readImage('dress.jpg');
$img->resizeImage(WIDTH, HEIGHT, Imagick::FILTER_LANCZOS, 1);
$img->writeImage('image1.png');
After "creating" an image resource with new Imagick() you are "working" in that resource. readImage() reads the image which is then present in the resource - after that you can resize the resource and write it out after the work is done.
See the documentation for more info Imagick::resizeImage()
With new Imagick(WIDTH, HEIGHT) you create an image resource with an empty canvas of the given size.

Related

ImageMagick - Convert PDF to JPG and Set Width

I am currently converting a PDF to a set of images using ImageMagick like this..
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('book.pdf');
// Writes an image
$imagick->writeImages('converted.jpg', false);
The resulting images are quite small, is there a way to set a defined width and then let the height work itself out?

How to display an image created by imagick php in html view?

I am using phalcon php framework. I have a blog in my application where a user can submit an image attached to his blog post, and it is displayed when viewing the post. I want to display this image on the index page of the blog where each post is listed, however, i want it to be a thumbnail to reduce its size. I was able to use imagick to save a thumbnail at a directory and load the thumbnail from there, however, i need to avoid saving the thumbnail and generate the thumbnails every time just to display them.
Here is the code I used to create the image with imagick
public function thumbnail($img)
{
$maxsize = 100;
$image = new Imagick($img);
// Resizes to whichever is larger, width or height
if($image->getImageHeight() <= $image->getImageWidth())
{
$image->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
}
else
{
$image->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
}
// Set to use jpeg compression
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
// Set compression level (1 lowest quality, 100 highest quality)
$image->setImageCompressionQuality(75);
// Strip out unneeded meta data
$image->stripImage();
// Writes resultant image to output directory
$image->writeImage($uploaddir.'/thumbs/'. basename($_FILES['photo']['name']));
// Destroys Imagick object, freeing allocated resources in the process
$image->destroy();
}
I tried just to remove the last two lines where the image is saved and then the imagick object is destroyed, but couldn't go on to display the image in the index view.
Step 1 - Use the buffer.
Step 2 - Unlink the already saved image after you get its base64 code.
For example consider the following code snippet:
$im = new Imagick();
$im->setResolution(300,300);
$im->readimage('path_to_the_file.pdf');
$im->setImageFormat('jpeg');
$im->writeImages('file_name_to_save.jpg', false);
$im->clear();
$im->destroy();
$dest = imagecreatefromjpeg ('file_name_to_save.jpg');
//Step 1
ob_start();
imagejpeg($dest);
$image_data = ob_get_clean();
imagedestroy($dest);
$img_source = base64_encode($image_data);
//Step 2
unlink('file_name_to_save.jpg');
Now in the $img_source variable you have the base64 of the image that you can use it like this:
'<img src="data:image/jpg;base64,'.$img_source.'" style="max-width: 100%; max-height:100%;" />';
I'm using something similar to this:
public function thumbAction() {
// Generate thumb from image & save it on disk
$image = $this->thumbnail(); // well can be anything that return Imagick
$this->response->setHeader('Content-Type', 'image/jpg');
echo $image;
}
Anyway in above example you don't need to save the image but only echo created thumb to the user.
Also I'm storing an image on some path like thumbs/a/image.jpg. I've configured nginx to check for existing file and if file does not exists it calls Phalcon script.
In Phalcon app I have a route that points to any /thumbs* path to above action. On first call the image is saved on the path thumbs/a/ so on the next call Nginx server returns that image instead of calling PHP.

WordPress grayscale specific image size

What to do: add a new image size and grayscale this specific size.
What I've done so far:
I've been searching around and found this helpfull URLs:
wpbeginner, Ryan Berry. The second solution creates a grayscale image for each size on every new image and is nearly what I'm searching for.
The Problem: I know how to add a new image size and the above functions creating grayscale images. What I don't know is how to add this filter just to a specific size.
Using the code from wpbeginner, it seems it only grayscales image which is specified as themename-bw-image, as seen from here (line 04):
$file = trailingslashit($file['path']).$meta['sizes']['themename-bw-image']['file'];
So I suppose if you don't want the image to get grayscaled, you could add a new image size:
add_action('after_setup_theme','themename_bw_size');
function themename_bw_size() {
add_image_size('themename-bw-image', 100, 100, true);
add_image_size('other-nongrayscaled-image', 150, 150, true);
// etc...
}

resizing an image with php and imagemagick take too much time

After several test i decided use scaleimage method of imagick for reduce images, the problem is because i want to get a square image and some of the images to process are rectangular.
To make it square i placed a blank square image as background after the scaled image, this point took me 4 second per image and it's too much.
// Create an image
$scaledimage = new Imagick();
// Set as black
$scaledimage->newImage(800,600,'black');
//Scale the image
$scaledimage->scaleImage(500,500,true); // I need the same aspect
// Create an image
$image = new Imagick();
// Set as white
$image->newImage(500,500,'white');
// compose an image onto another
$image->compositeImage($scaledimage,Imagick::COMPOSITE_DEFAULT,62,0);
// Clone
$scaledimage = clone $image;
Do you known how could i do to make the scaled image square without use this approach.

How do I resize an existing image (not create a new copy

Everything I can find online about re-sizing an existing image with PHP seems to be about taking an existing image and creating a resized thumbnail (unless I'm missing something). I already have the existing image on the server and i want THAT image (not a copy of that image) to be re sized. I want the quality to be decent so I'm thinking: imagecopyresized
http://php.net/manual/en/function.imagecopyresized.php
This is what I tried:
imagecopyresampled($sourceImageFilePath, $sourceImageFilePath, 0, 0, 0, 0, $resizedWidth, $resizedHeight, $origWidth, $origHeight);
but I get a supplied argument is not a valid Image resource error.
I know if I was creating a new resized image from the existing I would need this:
$thumb = imagecreatetruecolor($resizedWidth, $resizedHeight);
But I dont want to create NEW image but resize an existing one.
Thoughts?

Categories