get and set image dpi - php

I need some info about images inside cardimg folder.
$arr = glob("../cardimg/*.jpg");
foreach ($arr as $item){
list($width, $height) = getimagesize($item);
echo $width . '<br>';
echo $height . '<br>';
}
Works fine for width and height, but don't know how to see if an image is 72 or 300 dpi?
my php is 7.0.20 and using
print_r(imageresolution($item));
result is error - Call to undefined function
Also is there a way to change 300dpi to 72dpi using php?

This is straight from PHP.net for imageresolution(). (PHP 7 >= 7.2.0)
<?php
$im = imagecreatetruecolor(100, 100);
print_r(imageresolution($im));
imageresolution($im, 300, 72);
print_r(imageresolution($im));
?>
This should get you what you need once you update your php version:
$im = imagecreatefromstring(file_get_contents($path));
print_r(imageresolution($im));
imagedestroy($im);
Here is a link on how to get gd library up and going.. I'm sure this varies on server setup hosted or not, I don't know. But should help I imagine.
Enable GD support in PHP

Related

PHP Imagick don't use custom font > convert does

It's driving me insane.. Just searched for hours to find a solution. But nothing really found. So hopefully anyone can help me.
I'm trying to create an customized image on the fly. I use this to create the images: https://github.com/Treinetic/ImageArtist
Everything works fine except the font. This is the snippet from the class:
$im = new \Imagick();
$background = new \ImagickPixel('none');
$im->setBackgroundColor($background);
$im->setFont($font->getPath());
$im->setPointSize($writer->getSize() * (0.75));
$im->setGravity(\Imagick::GRAVITY_EAST); //later we will have to change this
$width = $writer->getWidth();
$height = $writer->getHeight();
$text = $writer->getText();
$margin = $writer->getMargin();
$im->newPseudoImage($width, $height, "pango:" . $text );
$clut = new \Imagick();
$clut->newImage(2, 2, new \ImagickPixel($color->toString()));
$im->clutImage($clut);
$clut->destroy();
$im->setImageFormat("png");
$image = imagecreatefromstring($im->getImageBlob());
$template = $this->imageHelper->createTransparentTemplate($width+ (2*$margin),$height+ (2 *$margin));
$img = new Image($template);
$text = new Image($image);
imagedestroy($image);
imagedestroy($template);
return $img->merge($text,$margin,$margin);
The $font-getPath(); looks correct and echo /var/www/vhosts/example.com/dev-test/ImageArtist/src/lib/AssetManager/../../resources/Fonts/Gotham-Light.ttf
This is the full code
use Treinetic\ImageArtist\lib\PolygonShape;
use Treinetic\ImageArtist\lib\Text\TextBox;
use Treinetic\ImageArtist\lib\Text\Color;
use Treinetic\ImageArtist\lib\Text\Font;
use Treinetic\ImageArtist\lib\Overlays\Overlay;
use Treinetic\ImageArtist\lib\Image;
require('../vendor/autoload.php');
$main_image = new Image("ranks/main-image.png");
$textBox = new TextBox(720,40);
$textBox->setColor(new Color(0,0,0, 125));
$textBox->setFont(Font::getFont('./Gotham-Light.ttf'));
$textBox->setSize(42);
$textBox->setMargin(0);
$textBox->setText($_GET["name"]);
$main_image->setTextBox($textBox, 40, 620);
$main_image->save("./newImage.png",IMAGETYPE_PNG);
But the font specified is not used by Imagick. If i try to create an image from den command line via
convert -font /var/www/vhosts/example.com/dev-test/ImageArtist/src/lib/AssetManager/../../resources/Fonts/Gotham-Light.ttf -pointsize 72 label:Test test.gif
And... The Font is used on command line created image :/ The font file is reachable and readable via php but not used via Imagick ....
Does anyone has an idea why php imagick can't use my font but convert -font can?
The script is running on PHP-FPM 7.0.23 on an dedicated root server with nginx as an RPS.
Hope you can help me out.. It really drives me crazy :)
Thanks
Stanlay
SOLVED
Found out that $im->newPseudoImage($width, $height, "pango:" . $text ); cause the issue. Switching to $im->newPseudoImage($width, $height, "caption:" . $text ); and everything works fine.

php imagick convert pdf to png high quality

I'm trying to convert a PDF to a high quality PNG via Imagick, but the file keeps coming out fuzzy. Currently, I'm running the following options but can't find the right flags to get a clear PNG out of the conversion. The original PDF file is 8.5 x 11. Suggestions? Thanks!
$image = new \Imagick(storage_path('app/'.$path));
$image->setResolution( 200, 200 );
$image->scaleImage(1700,2200);
$image->setImageFormat( "png32" );
$image->writeImage(storage_path('app/'.$split[0].'.png'));
You need to set the resolution before reading the image because the image is rasterised when read, so it doesn't help to set the resolution afterwards - it's too late!
Try along these lines:
$imagick = new Imagick();
$imagick->setResolution(288,288);
$imagick->readImage('someFile.pdf');
$imagick_i = new Imagick();
$imagick_i->setResolution( 595, 842 );
$imagick_i->readImageblob($blob);
$imagick_i->setImageFormat( "png32" );
foreach ($imagick_i as $auxiliaryvalue) {
echo '<img src="data:image/png;base64,' . base64_encode($auxiliaryvalue->getimageblob()) . '" /><br>';
}

PHP - Place an image over another image

I want to place an image over another image using php. I have two images. One is a photo. And other one is a image, of fixed size, full white. We can call this as frame. So what i need is place the photo in the middle of the frame (white image) and then save it. Can anyone pls help me?
to do this kind of work, you need to use GD library or ImageMagic
this code work with GD library
$photo_to_paste="image_to_paste.jpg"; //image 321 x 400
$white_image="white_image.jpg"; //873 x 622
$im = imagecreatefromjpeg($white_image);
$condicion = GetImageSize($photo_to_paste); // image format?
if($condicion[2] == 1) //gif
$im2 = imagecreatefromgif("$photo_to_paste");
if($condicion[2] == 2) //jpg
$im2 = imagecreatefromjpeg("$photo_to_paste");
if($condicion[2] == 3) //png
$im2 = imagecreatefrompng("$photo_to_paste");
imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));
imagejpeg($im,"test4.jpg",90);
imagedestroy($im);
imagedestroy($im2);
that code will output:

PDF to JPG Imagic page selection

Loads of answers on how to do it for a command line
convert /path/to/file/file.pdf[3] output.jpg
great... but what if I am using in memory processing, I am generating PDF with PDFlib and then output its buffer to a function that I want to generate jpg preview of selected page. How? My code :
[...]
$buf = $pdf->get_buffer();
//$buff is just a PDF stored in a string now.
$im = new Imagick();
$im->readimageblob($buf);
$im->setImageFormat("jpg");
$im->setimagecompressionquality(60);
$len = strlen($im);
header("Content-type: image/jpeg");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=test.jpg");
echo $im;
This creates a jpeg but always returns last page of the PDF. I want to be able to choose which one will be converted. Is it doable without saving temporary files and using command line (exec('convert /path/to/file/file.pdf[3] output.jpg')) syntax?
Let me add that I tried
$im->readimageblob($buf[2]);
and it did not work :)
For the ones who is still searching for solution of reading specific page number from blob, please check this question Creating array populated by images from a PDF using PHP and ImageMagick
$img_array = array();
$im = new imagick();
$im->setResolution(150,150);
$im->readImageBlob($pdf_in);
$num_pages = $im->getNumberImages();
for($i = 0;$i < $num_pages; $i++)
{
$im->setIteratorIndex($i);
$im->setImageFormat('jpeg');
$img_array[$i] = $im->getImageBlob();
}
$im->destroy();
I'm loading the PDF binary into memory from Amazon S3 and then selecting the specific page I want using setIteratorIndex() followed by getImage()
function get_image_from_pdf($pdf_bytes, $page_num){
$im = new \Imagick();
$im->setResolution(150, 150);
$im->readImageBlob($pdf_bytes);
$im->setIteratorIndex($page_num);
$im = $im->getImage();
$im->setImageFormat('png');
return $im->getImageBlob();
}
version 3.0.1
being on the last image or first image in imagic object
$image_obj = new Imagick("test.pdf"); then you on last image in $image_obj
if you use
fp_pdf = fopen("test.pdf", 'rb');
$image_obj = new Imagick();
$image_obj -> readImageFile($fp_pdf);
then you on the first image in $image_obj
In second case to switch to last image you can do
fp_pdf = fopen("test.pdf", 'rb');
$image_obj = new Imagick();
$image_obj -> readImageFile($fp_pdf,2); // 2 can be any positive number?
then you on the last image in $image_obj
echo $image_obj->getNumberImages() // Returns the number of images in the object
then
if ($image_obj->hadPreviousImage)
$image_obj->previousImage() //Switch to the previous image in the object
}
or
if ($image_obj->hasNextImage()) {
$image_obj->nextImage()) //Switch to the next image in the object
}
e.g. if you have 6 images total and you need 4th then do from the end
$image_obj->previousImage()
$image_obj->previousImage()
$image_obj->setImageFormat("png");
header("Content-Type: image/png");
echo $image_obj;
EDIT: Another find is that you can
foreach($image_obj as $slides) {
echo "<br>".$Obj_img->getImageWidth();
//or wehatever you need to do.
}
EDIT 2: Very simple solution would be to use this function $image_obj->setIteratorIndex(4) count starts with zero.
It's not good news unfortunately, but I can definitively say that, as of time of writing, the ImageMagick (and PHP libraries) don't support the page notation that you're trying to use. (For people from the future finding this: I'm checking php-imagick-3.0.1 and imagemagick-6.6.0.4).
I'm trying to do the exact same thing as you, and I've just spent the last few hours trawling through the source, trying to figure out what it does and how it gets the pages, and it looks like it simply won't use it when reading from a stream (ie. the readBlob() call).
As such, I'm just going to be putting it in a temporary file and reading it from there instead. Not as elegant, but it'll work.

PHP GD Move Picture Up 1 Px

I need to take an image and move it upwards 1 px in certain situations for my code, but with what GD function would I use to do that? I couldn't find another question that asked this, so I asked it. But the middle of the picture is a number and the background is transparent, and the height and width is almost always different
Here's an example. The key part is imagecopymerge() function. play with it's 0,0,1,0 values.
<?php
$src = imagecreatefromgif($img);
list($w,$h) = getimagesize($img);
$sprite = imagecreatetruecolor($w,$h);
$trans = imagecolortransparent($sprite);
imagealphablending($sprite, false);
imagesavealpha($sprite, true);
imagepalettecopy($sprite,$src);
imagefill($sprite,0,0,imagecolortransparent($src));
imagecolortransparent($sprite,imagecolortransparent($src));
imagecopy($sprite,$src,0,0,1,0,$w,$h);
imagegif($sprite,$img);
imagedestroy($sprite);
imagedestroy($src);
?>

Categories