Fastest way to extract a crop from a JPEG file on demand? - php

Users on my website can click a portion of a photo to get a closeup of it.
I currently have a bit of JavaScript that fires off an AJAX call to a PHP script that uses ImageMagick to retrieve the relevant crop.
Could I be doing this better, outside of PHP? Using ImageMagick directly somehow, or something else?
Currently the files reside on the same server as the main website, but due to space restraints I'm in the process of moving them to a separate server, so will need to make a call between the two somehow.
The photos vary in size, some are 1600x1200 and only 200KB, others are 24MP+ 20MB+ originals.

Using ImageMagick, you have two options:
Crop the image while loading it. What is then loaded initially is a cropped image.
Load the image, then crop it. What is initially loaded is the complete image.
The first method is more efficient and faster.
This method is to append the image area information to the input image(s) in square brackets ([...]) like this:
convert wizard:[130x150+80+80] -resize 200% wiz-head.png
This will crop a piece of 130x150 pixels with an offset of 80 pixels from the top left corner of the original, built-in wizard: image. Here are both images side by side, wizard: (left) and the cropped section, resized by 200%:
If you wanted to crop a JPEG, you'd use something like:
convert some.jpeg[330x250+180+280] -resize 300% output.png

Related

How to change the shape of an image using PHP and imagemagick?

I need to manipulate images on the server-side via PHP. I use ImageMagick for that purpose, and there are a lot of great functions, but no documentation...
The issue is the next:
I have two images a png with a transparent background, and a jpg one;
I need to change the JPG image that has the same shape as the visible part of the PNG.
I don't want to crop the image, but to adjust to the PNG's visible part. Is that possible to do?
Example:

How can I resize an image then centralize it with a white background? Can it be done using php-Imagine library?

The effect I would like to generate is exactly like the example in
this StackOverflow thread: (Related Question)
1.Resize image
2.Keep proportion
3.Add or Fill none-image areas with white background
Here are three examples of this process below:
1. If original is square no matter 640*640 or 1024*1024, just resize it to target dimension e.g. 480*480 will work.
If the original input is vertical rectangular,the output should not be cropped
and still be centerlized as below (the red dash edge marker is just make it easier to see the background is white and the proportion is kept after image resized to 480*480 px)
if the original input is horizontal rectangular, output like below, white background, keeps proportion and image uncroped , thus without losing anything after the image processing.
So after I've clarified such a question,
I would like to know:
Is there a general name of such Custom Image Resize mentioned above?
would like to know how to achieve it using php image library Imagine?
is it doable using php-imagine, or have to switch to another library, e.g imagemagick?
P.S. If you would like to achieve the effect either in image-resizing or video resizing, you can use below FFMPEG single-line command.
(thanks to #Mulvya, yes below code applies both to videos and image formats)
[run below built-in code in ffmpeg console to achieve the mentioned resize effect]
[video resize]
ffmpeg -i "[raw video url or videofile location.mp4]" -vf "scale=iw*min(480/iw\,480/ih):ih*min(480/iw\,480/ih),pad=480:480:(480-iw)/2:(480-ih)/2:color=white" [save_path_and_filename].mp4
[image resize]
ffmpeg -i "[raw image url or imagefile location.jpg]" -vf "scale=iw*min(480/iw\,480/ih):ih*min(480/iw\,480/ih),pad=480:480:(480-iw)/2:(480-ih)/2:color=white" [save_path_and_filename].jpg
Sorry i don'T have full answer for you and it's too long for a comment, but this can be done natively using some maths and php's http://php.net/manual/en/ref.image.php
Check in the area of imagecopyresize() imageecreate() imagescale and so on. It a pretty complex lib so you will have to do some try and mistake to get where you want to go.
From experience you can:
create an empty image, imagecreatetruecolor()
change background color to white
take a scetion of an image and paste it in the new created image using imagecopyresample() imagecopyresize()
export this image ans gif, png, jpg. imagepng(), imagejpg().
add watermark
and ALOT more.
Sorry i don'T have a more complete answer but depending on your need it can take a few mitues/hours to make it perfect. have fun.
On top of it, if you know ffmpeg, you can make both work together and make awesome media outputs. Cheers to you

reduce jpeg filesize "on the fly" with php

I would like to lessen the image size (and quality) of a jpeg on the fly when a user is using a certain browser, OS or screen size (used to distinguish mobile from desktop users). How do I do this?
I imagine hat i'd call a script that would return the image e.g.
<img src="<?resize.php?file=test.jpg&quality=75?>"
and in my resize.php I would need to convert test.jpg to be the $image ressource for imagejpeg () with which I can set the image quality.
How do I go about the conversion? also will
echo imagejpeg($img,NULL,$quality);
result in displaying an image using above html snippet?
You would need to write the resizing routine, and then have it saved someone web accessable. And I would cache the file the first time you resize it so you don't have to resize it every time someone loads a page.
Two objections:
img src requires an URL, not an image itself (actually, can be a data URL, but that's so wrong especially when trying to compress)
why rerender images for each hit? That would be horribly slow and inefficient. Just prerender them and select one of the variants according to user's specs.
If you still want to resize on the fly, then have a script resize.php or similar that will output the image of the given size. The script has to set the correct Content-Type header (image/jpg, for your example). Then use this as a tag:
<img src="resize.php?file=test.jpg&quality=75">
(note: no PHP tags, this is just a link!)

PHP - Crop an image, and center the resulting image onto a background, using GD

I'm trying to make a simple app which gives a user a standard background/template image onto which they can place their logo/brand/whatever. I've had a look at a few jQuery plugins which allows the user to upload and crop an image, and most of them seem to work by posting the x and y coords, and the width and height, to the server.
I'm able to then use imagecopyresampled() to merge the image with the background image, but (a) the user's image seems to end up really skewed, even when I hard-code dimensions that I know should work, and (b) I need to position the uploaded image pretty much dead-center -- what's the best way to achieve this?
Any help? This is my first time using GD. I'll add my own code if required, but I'm messing with the different GD functions, so I've butchered the one I had before. Will reproduce it.
Thanks

Finding blank areas of an image file using ImageMagick

I need to be able to determine if a part of an image (outside of predetermined crop marks) contains any image content.
Is there a way with ImageMagick (specifically the php interface) to do this?
Scenario:
The canvas is 8.5x11 with .5 in margins on the top, left, and bottom edges and a 1 in margin on the right edge. The image needs to fit within the crop marks for printing.
Normally I use Photoshop actions to do this, but I am trying to automate the process.
Replace everything within crop marks with black/white rectangle, do a histogram of the resulting image and analyze it?
I can do this with command-line version of ImageMagick, but dont know how to express it with PHP api.

Categories