Measuring a string in PHP? - php

ASP has a MeasureString function that returns the width, in pixels, of a string given a certain font/size. Is this possible in PHP?

Yes, using GD library.
http://www.php.net/manual/en/function.imagettfbbox.php
this is for text rendered on an image.
Text width varies a bit depending on the browser, OS (font smoothing, different rendering)

Perhaps you could use the imagettfbbox function if you have GD installed

Related

Remove whitespace using Imagick and PHP, then save as transparent PNG

I've got an image and want to remove all whitespace around it, and then save it as a transparent PNG. I'm using Imagick in PHP to do so, but my script doesn't seem to function properly.
<?php
$im = new Imagick("http://images.icecat.biz/img/norm/high/14688888-2862.jpg");
$im->borderImage("#ffffff", 20, 20);
$im->trimImage(0.3);
$im->setImagePage($im->getImageWidth(), $im->getImageHeight(), 0, 0);
$im->setImageFormat("png");
header("Content-Type: image/" . $im->getImageFormat());
echo $im->getImageBlob();
?>
What do I need to do to remove all white (and close to white) areas at the borders? And when that is done, can I easily resize the image to crop all of the transparency?
The fuzz factor needs to be a quantum scaled value, not just for this function but almost all functions that take 'fuzz' as a parameter.
i.e. you need to scale it up to the quantum range.
$im->trimImage(0.3 * \Imagick::getQuantum());
Or if you are using an earlier version of Imagick that doesn't have that method, then instead do:
$range = $image->getQuantumRange();
$image->trimImage(0.3 * $range['quantumRangeLong']);
The reason for this is to allow precise control over the pixel matching. If the value was passed in as a float value in the range 0-1 it would not possible to have exact control over the value that was used for matching.
By instead using an integer value (for versions of Imagick that do not have HDRI enabled) it allows you to precisely control the values that are compared for the operation.
You need something like an autocrop based on pixel values, I think this will help:
http://www.imagemagick.org/script/command-line-options.php#trim
you might also like:
http://fmwconcepts.com/imagemagick/autotrim/index.php
source:
http://www.imagemagick.org/discourse-server/viewtopic.php?t=10843

PECL Imagick adaptiveSharpenImage not working?

I'm trying to sharpen my images using this function, but it's not working. Whatever values I pass in for radius and sigma I get an identical image (same file size even) out.
It returns a 1 suggesting no error. What might be going wrong here?
$photo = new Imagick(PHOTOS_DIR.$sFilename);
$photo->adaptiveSharpenImage(2,1); //4,2 ... 0,10, 0.5,0.5 - all give identical results
$guid = md5(uniqid(rand(),true));
$photo->writeImage(PHOTOS_DIR.'/temp/'.$guid.'.jpg');
I tried passing in imagick::CHANNEL_BLUE for the optional third parameter, it made no difference.
CentOS 6.5
PHP 5.5.12
pecl-imagick 3.1.2
imagemagick 6.5.4
The adaptiveImageSharpen function is really subtle in some circumstances. The images below are the source and then the sharpened one produced by:
$radius = 10;
$sigma = 2;
$imagick->adaptiveSharpenImage($radius, $sigma);
Source image
Sharpened image
Even at those relatively large values, the image is definitely sharpened but the effect is only noticeable when compared to the source image. The radius you choose will depend on the size of your source image, but ought to be at least a couple of pixels across. Alternatively you can set a radius of zero, and ImageMagick should choose an appropriate value for the image.
Depending on what you're trying to achieve with the sharpening you may be better off with and old fashioned unsharpMaskImage if adaptiveSharpenImage is always too subtle an effect for you.
Or it could just be broken on the very old version of ImageMagick you're using - can you try with the sample image and settings above.

Add round corners to a jpeg file

I am trying to add round corners to a jpeg file, but the problem is that after adding round corners, I am getting a black background color. Somehow I am not able to change it to any other color (white, transparent, red). It just simply shows black background where the image has rounded corners.
The code that I am using is:
<?php
$image = new Imagick('example.jpg');
$image->setBackgroundColor("red");
$image->setImageFormat("jpg");
$image->roundCorners(575,575);
$image->writeImage("rounded.jpg");
header('Content-type: image/jpeg');
echo $image;
?>
I cannot use png as the jpeg files are huge, about 5 MB, so if I used png, the file size would go up to 26 MB, even though the png adds transparent round corners.
Also the IMagick version that i am using is:
ImageMagick 6.6.2-10 2010-06-29 Q16 http://www.imagemagick.org
Also the output(image generated) will get printed so I don't know if css will work over here.
Sorry, I am trying to actually create a new jpeg file with rounded corners from an already existing jpeg file that doesn't have round corners this is actually a photograph taken from a camera, so there are multiple/too many colors so I can't use gif as well.
Also my site will only just generate the round corner image then afterwards it will get downloaded using a FTP program by the admin of the site and then using a system software will get printed, so in short my website will not be printing the image but rather just generate it
Try this:
<?php
$input = 'example.jpg';
$size = getimagesize($input);
$background = new Imagick();
$background->newImage($size[0], $size[1], new ImagickPixel('red'));
$image = new Imagick($input);
$image->setImageFormat("png");
$image->roundCorners(575,575);
$image->compositeImage($background, imagick::COMPOSITE_DSTATOP, 0, 0);
$image->writeImage("rounded.jpg");
?>
I may get downvoted, but I say let css deal with the corners and take some load off of your server :)
CSS rounded corners.
JPG doesn't have a transparent color(s) (alpha channels) in its palette.
The output image must use either PNG or GIF (or another image format that supports alpha channels).
setImageBackgroundColor is another option if you want an opaque background.
EDIT
Your comment reminds me that you could try to use the command line; shell_exec() will run a command line argument from PHP. The command in the ImageMagick API you'll need to start with is convert example.jpg, and then you can pass flags with the various parameters you want.
Since ImageMagick is already installed, it will work right away. You may need to point your system PATH to the ImageMagick directory where all of the executables are.
There's plenty of questions and forums dedicated to rounded corners with this method so I'll leave that up to you.
Here's a helpful tip though - there is a silly confusion with the convert command, since Windows also has a convert.exe that is rarely used, but will confuse your command line, so make sure you're calling the right convert. ;) To test if it's working, try convert example.jpg example.gif (which should convert your example to a gif).
To get output from your command line, finish all commands with 2>&1 which will pipe cmd output back into PHP.

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)

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.

Categories