How to resize image in PHP? - php

I often see some websites using the below code to resize image size.
?w=250
?w=150
?w=75
?w=50
Sample:
http://domain.com/customthumb/2013/08/07/72/getty380.jpg?w=250
http://domain.com/customthumb/2013/08/07/72/getty380.jpg?w=150
http://domain.com/customthumb/2013/08/07/72/getty380.jpg?w=75
http://domain.com/customthumb/2013/08/07/72/getty380.jpg?w=50
I already search in Google, but I can't find the solution.
Do you know how to do it in PHP?
This is the demo:
Original size don't using "?w=75" or other size:
_http://images.detik.com/customthumb/2013/08/09/722/uang4depan.jpg
width 75px : _http://images.detik.com/customthumb/2013/08/09/722/uang4depan.jpg?w=75
width 100px : _http://images.detik.com/customthumb/2013/08/09/722/uang4depan.jpg?w=100
width 110px : _http://images.detik.com/customthumb/2013/08/09/722/uang4depan.jpg?w=110
width 150px : _http://images.detik.com/customthumb/2013/08/09/722/uang4depan.jpg?w=150
Note: remove underscore "_" in front of URL.

I don't know what those URLS are for, but many PHP users use the Imagick functions to manipulate images.

There are several ways, considering what's your output.
First of all, HTML can do this so if you use
print "<img src='...whatever...' width='$x'>"
this will do the trick. Now if you're writing with a header like image/jpg, which means you're being called from an IMG tag and your script returns the image itself - well, use GD library and imagecopyresampled(). But that's a thing I'll explain in detail only if you really need it.

Related

Sharp image resize in php and why it differs from result in browser (<img />)

How to downscale image in php so that result is sharp? I have tried imagecopyresampled, but result is very blurry.
If I'm changing width on image in browser by adjusting width attribute on img tag, result is very appealing. Is there any way to get same effect with php?
Update
Looks like I can achieve what I desire if I make icon larger than what I will actually display.
Sample: http://codepad.viper-7.com/0AiLUT or http://pastebin.com/V0PqCMpg

PHP gd imagettftext() height

Is there a way to get PHP imagettftext() font height?
I know that we can get width by imagettfbbox(). Can we get the height too?
Yes, you evidently can simply be looking at the documentation. Scroll down and use your browsers find function to search for the following:
jodybrabec at gmail dot com
Below his name he provides a nice cusotm function for you to use.
(got a link actually right here, no need to use the find function: http://php.net/manual/en/function.imagettfbbox.php#105593)

How to white pad/border an image with magickwand?

I have an image that is 180x240 and I want to pad/border it with white until it fits the size 360x240, so the original content from image won't get distort.
http://www.magickwand.org/ - I can't find any suggestive function in the documentation
I do not use Magickwand but with Imagemagick there is an operator called extent which does what you want. But I can not see any mention of extent in the documentation you linked.
You may have to create a white canvas of 360x240 and composite your image onto it.
Alternativly could you use MagickBorderImage and have the border set to 0 or 1 on the height and 90 on the width?

Get the width and height of a string in pixel (PHP)

Is there any good function to calculate the pixel width and height? Can imagettfbbox be used to do that? I mean which ttf file is needed for the different fonts used by different browsers?
Thx
As PHP run's on the server there is no way to do that with PHP alone. The size of the text depends on the operating system, the browser (and the browser settings like zoom) of the client. You could however use javascript to get the size of an element once its rendered.
height = document.getElementById("elementId").style.height; // Will return 176px for example
width = document.getElementById("elementId").style.width; // Will return 176px for example
Hope this helps
There is a function (imagefontwidth and imagefontheight), but they only return the size of one character using the built-in fonts (which are all fixed-width). You'll get the width/height of the entire string by multiplying the number returned by the number of characters.
I don't know of any way that lets you get the width or height of a string using a custom font, but I'm interested in this too.
Hope this helps.

PHP/ImageMagic Get the "shadow" of an image

I would like to script PHP/ImageMagic in order to produce the "shadow" of a given image. Like in this example (done manually in GFXeditor) :
original shadow
alt text http://uppix.net/d/a/f/dc82fce795fc4af20170080b09c9a.png ==> alt text http://uppix.net/8/9/9/b1e9df4b2858c40081771961e028d.png
Note: All the originals images will be on a white background like in the example.
I've check the ImageMagic documentation but I haven't found anything useful yet. Does anyone know if it can be done in PHP/ImageMagic ? If so how ?
Use convert with -threshold option?
EDIT: oops... from PHP? Imagick::thresholdImage?
I wonder if it isn't more like a mask than a shadow? In the context of IM, shadow looks like a blurry copy of the image.
Try the edge detection chapter in the ImageMagick examples.

Categories