change image format using Imagick() - php

I want to change image file as jpeg format as follows:
$img_real_path = realpath(".") . "/" .$dest;
echo $img_real_path;
$image = new Imagick();
$image->readImage($img_real_path);
$image->setImageFormat("jpeg");
But image format is same as it was. What am I doing wrong?

You're not using $image->imageWriteFile()
Add this to your code:
$image->imageWriteFile (fopen ($image_real_path, "wb"));
It should look like this:
$img_real_path = realpath(".") . "/" .$dest;
echo $img_real_path;
$image = new Imagick();
$image->readImage($img_real_path);
$image->setImageFormat("jpeg");
$image->imageWriteFile (fopen ($image_real_path, "wb"));

Related

convert ico to png formatter in vue laravel

i am trying to convert ico format file to png format
$media_url ="https://static.licdn.com/scds/common/u/images/logos/favicons/v1/favicon.ico"
$filePath = public_path('media_downloads').'/pnff.png';
file_put_contents($filePath, file_get_contents($media_url));
$s3 = \Storage::disk('s3');
$res = $s3->put($filePath, file_get_contents($img), 'public');
$downloadUrl = \Storage::disk('s3')->url($filePath);
i got s3 url but the file is not opening i think it is corrupted how can i change the image content type while saving to a folder also in s3
i have done ico to png convertion by using Imagick Thanks #aynber #Chris Haas for the guidance
use Imagick;
$file = $media_url;
$extension = pathinfo(parse_url($file, PHP_URL_PATH), PATHINFO_EXTENSION);
$filename = 'linkedin_thumbnail' . time() . '.' . $extension;
$local_file_path = public_path('media_downloads') . '/' . $filename;
$contents = file_get_contents($file);
file_put_contents($local_file_path, $contents);
$image = new Imagick();
$image->readImage($local_file_path);
$image->cropThumbnailImage(100,100);
$image->setImageFormat("png");
header("Content-type: image/png");
$filePath2 = public_path('media_downloads').'/'.date('ymdhis').'.png';
$image->writeImage( $filePath2 );
$media_url = $filePath2;
unlink($local_file_path);
unlink($filePath2);

IMagick RGB to CMYK corrupt conversion

I'm converting images from RGB to CMYK with IMagick in PHP.
During conversion some images get black grids on them.
Code:
$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage(SITE_ROOT . 'userfiles/image/products/' . $image);
$IMagick->negateImage(false, Imagick::CHANNEL_ALL);
$IMagick->setImageColorspace(13);
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebCoatedSWOP.icc');
$IMagick->profileImage('icc', $icc_cmyk);
unset($icc_cmyk);
$IMagick->setImageColorspace(12);
$IMagick->writeImage (SITE_ROOT . 'userfiles/image/products/cmyk/' . $image);
Images:
Original
Converted
I'm converting around 80 images in a loop and most of them are OK.
Any idea why it happens?
EDIT:
Working code:
$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage(SITE_ROOT . 'userfiles/image/products/' . $image);
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebCoatedSWOP.icc');
$IMagick->profileImage('icc', $icc_cmyk);
unset($icc_cmyk);
$IMagick->transformImageColorspace(12);
$IMagick->writeImage (SITE_ROOT . 'userfiles/image/products/cmyk/' . $image);
setImageColorspace should only be used when creating new images, either through Imagick::newPseudoImage or rendering an ImagickDraw instance into an image.
For existing images, the correct method to change the colorspace of the image is Imagick::transformImageColorspace.

getting image dimensions in imagick

I'm trying to get the dimensions of images that i converted with imagick. This is my code:
$imagick = new \Imagick();
$imagick->setresolution(300,300);
$imagick->readimage($this->uploadPath . '/original/test.pdf');
$imagick->writeImages($this->uploadPath . "/large/preview-%d.jpg", false);
So i'm reading a multipage pdf file and i'm converting it to seperate jpg files.
What i want is to retrieve the jpg file dimensions from each image. Can i do this in an easy way? Or do i first need to write the images to the disc and then loop through each of them again?
What about Imagick::getImageGeometry? (http://www.php.net/manual/en/imagick.getimagegeometry.php)
In your case it should look something like this:
$imagick = new \Imagick();
$imagick->setresolution(300,300);
$imagick->readimage($this->uploadPath . '/original/test.pdf');
$geometryInfo = $imagick->getImageGeometry();
// now $geometryInfo is an associative array with 'width' and 'height' items in it
$imagick->writeImages($this->uploadPath . "/large/preview-%d.jpg", false);
Update:
If you want to read concrete page from multipage PDF, try to use such trick:
$pageIndex = 0; // you can create "for/foreach" loop to go through all pages
$imagick->readimage($this->uploadPath . '/original/test.pdf[' . $pageIndex . ']');

adding jpg to a variable

I am creating a variable from an XML file ($image) and I need to create a new variable that combines $image with '.jpg'. I've tried:
$image = $record->getElementsByTagName( "name_id" );
echo $image.".jpg";
$image = $image->item(0)->nodeValue;
But this doesn't add .jpg to the variable in the script. The reason I need to do this is the XML file doesn't have the jpg file extension only the image reference as a sequence of numbers. Any ideas?
If $image is a string, just concoct the strings together with ..
You can see this as an example:
<?php
$img = hello;
$imgfinal = $img . ".jpg";
echo $img;
?>
As expected the output is hello.jpg. Hope this helped.
Are you looking for:
$image = $record->getElementsByTagName( "name_id" );
$image = $image->item(0)->nodeValue;
$image = $image . ".jpg";
Now $image should contain the filename with the file extension

Converting multipage pdf to multi images

I am trying to convert a multipage PDF File to images by using PHP Image magic extension.The problem is that instead of getting images corresponding to each page of the file, I am getting the last page of pdf as the output image. Here is the code:
$handle = fopen($imagePath, "w");
$img1 = new Imagick();
$img1->setResolution(300,300);
$img1->readImage(path to pdf file);
$img1->setColorspace(imagick::IMGTYPE_GRAYSCALE);
$img1->setCompression(Imagick::COMPRESSION_JPEG);
$img1->setCompressionQuality(80);
$img1->setImageFormat("jpg");
$img1->writeImageFile($handle);
What am I doing wrong?The convert command on commandline with the same parameters works.
Try something like this instead:
$images = new Imagick("test.pdf");
foreach($images as $i=>$image) {
$image->setResolution(300,300);
//etc
$image->writeImage("page".$i.".jpg");
}
Try writeImages function. It creates each page as one image and it gives file names for multiple images like this: yourimagename, yourimagename-1, yourimagename-2.... It increases automatically from 0 to your numberofpagesinPdf-1.
The code looks like this:
$imagick = new Imagick($file_handle);
$imagick->readImage();
$imagick->writeImages($yourImagename.'.jpg', false);
This will work for pdf having multiple pages as well as the single page.
$pdf_file = 'path/to/pdf/file.php';
$image = new imagick();
$image->setResolution(300,300);
$image->readImage($pdf);
$image->setImageFormat('jpg');
// Set all other properties
$pages = $image->getNumberImages();
if ($pages) {
foreach($image as $index => $pdf_image) {
$pdf_image->writeImage('destination/path/' . $index . '-image_file.jpg');
}
} else {
echo 'PDF doesn\'t have any pages';
}
Try something like this if you know number of pages of your pdf:
$images = new Imagick();
foreach ($pages as $p){
$im->readImage($PdfFile."[".$p."]"); //yourfile.pdf[0], yourfile.pdf[1], ...
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(82);
$im->setImageFormat( "jpg" );
//...
$image_out = "image_".$p.".jpg";
$im->writeImage($image_out);
}
$im->clear();
$im->destroy();
If you dont know number of pages, you could do something like this:
$images = new Imagick();
$im->readImage($PdfFile);
$pages = (int)$im->getNumberImages();
this worked for me
$file = "./path/to/file/name.pdf";
$fileOpened = #fopen($archivo, 'rb');
$images = new Imagick();
$images->readImageFile($fileOpened);
foreach ($images as $i => $image) {
$image->setResolution(300, 300);
$image->setImageFormat("jpg");
$image->setImageCompression(imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(90);
$image->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$data_blob = $image->getImageBlob();
$ouput="./path/images/files/page" . $i . ".jpg";
file_put_contents($ouput, $data_blob);
}
#fclose($fileOpened);
I hope I can help you too

Categories