Check if sub image exists in main image with PHP? - php

Using PHP, how can I check whether an image exists within another?
Example:
How can I check whether subimage.jpg exists within mainimage.jpg, using PHP?
subimage.jpg
mainimage.jpg

Try this
$img1 = md5(file_get_contents($img1));
$img2 = md5(file_get_contents($img2));
And compare both

The Imagick class , Check this link
Using Imagick::compareImages() to Compare images and display the reconstructed image .
<?php
$image1 = new imagick("image1.png");
$image2 = new imagick("image2.png");
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");
header("Content-Type: image/png");
echo $result[0];
?>

Related

How can Fopen read png files in php

I was working on something and I needed the data out of a png file and I can only use PHP. Don't ask why just give me the answer lol
Lets suppose below is your image.
You have to define its url in a variable using the below line
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
after that you can use file_get_content function to get data/content of image.
$image_content = file_get_contents($image);
also, if you want to convert it into base64 string, you can use base64_encode function.
$image_base64Data = base64_encode($image_content);
Whole Code will be ......
<?php
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
$image_content = file_get_contents($image);
$image_base64Data = base64_encode($image_content);
?>

Remove background after comparing two images with Imagick

i'm new using imagick with php and i have a problem:
I'm comparing two images with the compareImages() function. I'm using the same code as in the documentation page:
<?php
$image1 = new imagick("image1.png");
$image2 = new imagick("image2.png");
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");
header("Content-Type: image/png");
echo $result[0];
?>
This code is working right, but the result image has a background with the original image with a bit of opacity.
I've been searching and I found the result that I want but with imagemagick commands:
http://www.imagemagick.org/Usage/compare/
My current result is like the image of the first command ("compare bag_frame1.gif bag_frame2.gif compare.gif") and I want a result like the shown in this command:
compare bag_frame1.gif bag_frame2.gif \
-compose Src compare_src.gif
Is there any way of doing this with the compareImages() function of imagick?
Thanks
You'll need to use Imagick::SetOption before reading the first image.
<?php
$image1 = new imagick(); // Init empty object
$image2 = new imagick("image2.png");
// Set Lowlight (resulting background) to transparent, not "original image with a bit of opacity"
$image1->setOption('lowlight-color','transparent');
// Switch the default compose operator to Src, like in example
$image1->setOption('compose', 'Src');
// Now read image
$image1->readImage("image1.png");
// Result will not have a background
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);

php save image with specific file name not overwriting each other

I hope someone can help me with this php code.
At the moment its just saving an image with the file name "img.png" to the server but with every time a new canvas screenshot is taken the image is just overwritten.
My aim is to create a new unique (like numbered chronological by time taken) file name for the images with every new screenshot and save it on the server.
Here the php code so far:
$data = $_REQUEST['base64data'];
echo $data;
$image = explode('base64,',$data);
file_put_contents('img.png', base64_decode($image[1]));
Thank you.
regards
Try
$filename = 'img_'.date('Y-m-d-H-s').'.png';
file_put_contents($filename, base64_decode($image[1]));
This will save your file with a filename containing the current date and time, e.g.
img_2013-09-19-21-50.png
Try using a session variable to increment a counter like so:
<?php
session_start();
if(!isset($_SESSION['counter'])){
$_SESSION['counter'] = 0;
}
$_SESSION['counter']++;
$data = $_REQUEST['base64data'];
echo $data;
$image = explode('base64,',$data);
file_put_contents('img'.$_SESSION['counter'].'.png', base64_decode($image[1]));
?>
There's several ways to do it, but the easiest is just to add a timestamp/datestamp to the image name. Format the name as you want.
$img_name = 'img'.date('YmdHisu').'.png'; // Date & time with microseconds
$img_name = 'img'.time().'.png'; // unix timestamp
Leave the base64data structure use this one it will work fine.
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
$image = explode(".", $fileName);
It will give a random number to each image file.
either create a UID using uniqid() function for the filename or create a folder with the name of the username who is uploading the file and leave the original filename. The disadvantage of the first one is that you will have to save the original filename somewhere to show to the user.
https://stackoverflow.com/a/4371988/2701758
**
/* simply for local time first give your continent then '/' then your country's
capital.
*/
date_default_timezone_set('Asia/Dhaka');
$now = new DateTime();
$now = $now->format("Y-m-d H:i:s.u");
$new_name = $now.$image;
/*what you want to add just write with dot,such
$new_name = 'img'.$now.$image;
*/
**

How to set color of a pixel using imagick for php (imagemagick)?

I have get the image pixel of an image at the particular point using getImagePixelColor.
$pixel = $image -> getImagePixelColor($x,$y);
Now I have modified that pixel's color using some method and now I want to set the new color of that pixel.
How can I do ?
There is a setColor function. But I got the pixel from the Imagick class. But the setColor function is in the ImagickPixel class. So how can I do it ?
ImagickPixel::setColor() is the correct function but it is also necessary to sync the pixel Iterator so your manipulations are written back to the image.
Here is a short yet (almost) complete example that reads an image file, manipulates each pixel, and dumps it to a browser:
$img = new Imagick('your_image.png');
$iterator = $img->getPixelIterator();
foreach ($iterator as $row=>$pixels) {
foreach ( $pixels as $col=>$pixel ){
$color = $pixel->getColor(); // values are 0-255
$alpha = $pixel->getColor(true); // values are 0.0-1.0
$r = $color['r'];
$g = $color['g'];
$b = $color['b'];
$a = $alpha['a'];
// manipulate r, g, b and a as necessary
//
// you could also read arbitrary pixels from
// another image with similar dimensions like so:
// $otherimg_pixel = $other_img->getImagePixelColor($col,$row);
// $other_color = $otherimg_pixel->getColor();
//
// then write them back into the iterator
// and sync it
$pixel->setColor("rgba($r,$g,$b,$a)");
}
$iterator->syncIterator();
}
header('Content-type: '.$img->getFormat());
echo $img->getimageblob();
->getImagePixelColor() returns an ImagickPixel object anyways, so $pixel->setColor(...); is all you need:
Ref: http://php.net/manual/en/imagick.getimagepixelcolor.php

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.

Categories