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.
Related
Lets say I have an arrays of pixels. Basically an array of this data {x,y,r,g,b,a} for each pixel.
How would I go about converting all this data into an actual image file (png preferably)?
Could not find a solution. Any help would be very appreciated.
I had some time to code up a little example. You should be able to see and note that:
the red component increases towards the bottom of the image
the green component increases towards the right of the image
the blue component is absent
the alpha channel is random and between 0 (opaque) and 127 (fully transparent)
// Define width and height
$w=800;
$h=600;
// Create truecolour image so we can have infinitely many colours rather than a limited palette
$img=imagecreatetruecolor($w,$h);
imagesavealpha($img,true);
imagealphablending($img,false);
// Iterate over all pixels
for($y=0;$y<$h;$y++){
for($x=0;$x<$w;$x++){
$r = round(255*$y/$h);
$g = round(255*$x/$w);
$b = 0;
$alpha = rand(0,127);
$color = imagecolorallocatealpha($img,$r,$g,$b,$alpha);
imagesetpixel($img,$x,$y,$color);
}
}
// Save result
imagepng($img,"result.png");
I'll admit I haven't actually used this API, but looks like PHP has what you're looking for.
You create an image identifier with imagecreate or one of the related functions, then color in each pixel with imagesetpixel, using a color identifier created with imagecolorallocatealpha. From there you should be able to output as a PNG with imagepng.
It's worth noting that this image library seems to support drawing lines and shapes and other structures higher than the per-pixel level, so I'd also look into whether your code necessarily needs to build a big pixel array, rather than drawing the image some other way.
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
I did found a topic similar to this, but I do not know if the solution is the same. So here is my question:
I'm using the GD functions to bild a web card generating program. The thing is that the card's backgound is generating by the $image = imagecreatefrompng(); function.
The card need's also a $cardname as "title" and a $desription as desription. For that I used the imagettftext(); function. But there is a problem, the card's size is 333x485, I need the text to be resized in order to fit in the background without resizing its height, but only the width!
To be more to the point, the $cardname should have width = 240 and height = 34, but if it doesn't fit, it goes off the background, I need a function that will resize its width in order to fit in 240px and leave the height to 34px always!
To understand it more look here: http://yugiohcardmaker.net. in the "name" you can add as much text you like, it will always fit in and in the right width and height!
I'm not going to try and code this as it will take too long, but here's the basic process:
Get the size of the bounding box for your text with imagettfbbox();
Create a new image with imagecreatetruecolor();
Write your text into your new image with imagettftext();
Use imagecopyresampled() to copy the new image with your text to your existing card, setting the parameters to shrink the width but not the height.
Note: the bounding box parameters returned by imagettfbbox()) can be fiddly to work with
You'll also need to be careful about alphablending and background colors to ensure that only your text pixels are copied.
Good luck!
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.
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