Call to undefined method Imagick::writeImageFile() - php

I've a problem with Imagick - PHP.
I've this code :
function createImages ($src, $dst, $width, $height)
{
$image = new Imagick($src);
$image->stripImage();
$color = new ImagickPixel();
$color->setColor('rgb(255,255,255)');
if(resizeimage($image, $width, $height)) {
$image->borderimage($color,
intval(($width - $image->getimagewidth()) / 2),
intval(($height - $image->getimageheight()) / 2));
$image1 = new Imagick();
$image1->newImage($width, $height, new ImagickPixel('white'));
$image1->setImageColorspace($image->getImageColorspace());
$image1->compositeImage($image, $image1->getImageCompose(), 0, 0);
$image1->setImageCompressionQuality(90);
$image1->setimagecompression(Imagick::COMPRESSION_JPEG);
$image1->setformat('jpeg');
$fileHandle = fopen($dst, "w");
$image1->writeImageFile($fileHandle);
$color->destroy();
$image1->destroy();
}
$image->destroy();
return true;
}
And this error :
Fatal error: Call to undefined method Imagick::writeImageFile()
I don't understand why all other methods work good, but not writeImageFile(). Can you help me please?

The writeImageFile function is available in ImageMagick version 6.3.6, so perhaps your version is not recent enough? There is also writeImage which may be a usable alternative in your case: http://www.php.net/manual/en/imagick.writeimage.php

Related

php thumbnail image problem (some images flipped or rotated)

i use php to create thumbnail of image on upload. But i have some issues. Sometimes thumbnail is fliped vertically or horizontally or 90º rotated ... and i dont know why... some images are correct some not.
Don´t you know, where could be problem?
UPDATE:
imagecreatefromjpeg load image wrong rotated...
I´m using this code...
class ThumbImage
{
private $source;
public function __construct($sourceImagePath)
{
$this->source = $sourceImagePath;
}
public function createThumb($destImagePath, $thumbWidth=100)
{
$sourceImage = imagecreatefromjpeg($this->source);
$orgWidth = imagesx($sourceImage);
$orgHeight = imagesy($sourceImage);
$thumbHeight = floor($orgHeight * ($thumbWidth / $orgWidth));
$destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight);
imagejpeg($destImage, $destImagePath,100);
imagedestroy($sourceImage);
imagedestroy($destImage);
}
}
and call it with:
$objThumbImage = new ThumbImage($uploadFilePath);
$objThumbImage->createThumb($uploadFileThumbPath, 640);

PHP - Cannot display image because it contains errors

Good day guys,
I inherited some legacy code that reads a BLOB from the database, parses it and returns the jpeg stream to be displayed in the browser. The legacy code used to run on PHP 5.5, it has since been deployed on a PHP 7.1 server where I am running into the issue.
I have made sure that the gd library for php is installed and loaded in Apache, I can verify that it is enabled and working with the php info file. I have also dumped the value from the DB and made sure the blob is a valid image, which it is. For some reason or another I cannot get the function to provide the image.
Here is the code for the function after the data has been retrieved from the DB and stored in $image
$image = imagecreatefromstring($image);
$originalWidth = imagesx($image);
$originalHeight = imagesy($image);
header ( 'Content-type:image/jpeg' );
if($originalWidth == $width && $originalHeight == $height)
{
if($thumb && $width >= $originalWidth) echo imageautocrop($image);
else echo imagejpeg($image);
}
else
{
$ratio = $originalHeight/$originalWidth;
$height = $width*$ratio;
$height = round($height);
$new = imagecreatetruecolor($width, $height);
imagecopyresampled($new, $image, 0, 0, 0, 0, $width, $height, imagesx($image),imagesy($image));
imageantialias($new, true);
if($thumb) imageautocrop($image);
else imagejpeg($new, null, $quality);
imagedestroy($new);
}
I have tried using the exact dimensions as well as the cropped dimensions, neither of which work.

can't resize an image using php

I have a php code to get an image and resize it. Can't get the output as expected.. Please help me figure out what the exact problem is..!!
<?php
$picture_source = 'image.png';
if ($picture_source != null){
$img1=file_get_contents($picture_source);
$new_img1 = resizeImage($img1, 200, 200);
file_put_contents("i1.png", $new_img1);
}
function resizeImage($img,$newwidth,$newheight) {
list($width, $height) = getimagesizefromstring($img);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $thumb;
}
You've got some confusion with your variable names and types. You're trying to deal with a filename, the file contents, and an image resource at the same time, and you're using the wrong ones for a few of the functions.
imagecopyresampled takes two image resources (source and destination), but you're passing it the file contents in place of the source.
file_put_contents takes a string for the file contents, but you're passing it the resized image resource.
PHP has native functions for reading the image dimensions and creating an image resource from a filename, so you shouldn't need to ever have the source file contents available as a string.
If you change a few of the variable names and function calls around, you end up with:
<?php
$sourceFilename = 'image.png';
if ($sourceFilename != null){
$newImg = resizeImage($sourceFilename, 200, 200);
imagepng($newImg, "i1.png");
}
function resizeImage($imgFilename, $newWidth, $newHeight) {
list($width, $height) = getimagesize($imgFilename);
$img = imagecreatefrompng($imgFilename);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $thumb;
}
Also, you should turn on PHP notices and warnings in your development environment - they will have been trying to help you.

PHP Imagick black areas after converting PDF to JPG

I'm using Imagick to generate JPG thumbnails for PDFs files but some of them generate with black areas (http://i.imgur.com/fKBncKw.jpg) – I'm assuming it's caused by transparency in the PDFs but can anything be done about it?
Code I'm using to generate these:
$imagick = new Imagick($filename);
$imagick->setIteratorIndex(0);
$imagick->setImageFormat('jpg');
return $imagick->getImageBlob();
Is there a way to flatten the PDF and/or add a white background so that the black areas don't appear?
Here is a solution which will only work for PNG to JPG.
This code adds a white background to the transparent areas in PNG and converts it to JPG.
What it does?
This code takes all PNG images from a folder, converts them to JPG's with white backgrounds and saves them in another folder.
<?php
ini_set('max_execution_time', 3000);
$dir = 'transparent/';
$arr = scandir($dir);
for($i=0;$i<count($arr);$i++)
{
if($i==0 || $i==1)
{
}
else{
$input_file = "transparent/".$arr[$i];
$output_file = "White/".str_replace('.png','.jpg',$arr[$i]);
$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);
}
}
?>
Its Image Processing and GD in PHP.
PHP Manual
Hope it helps, you can change it as you want.
Try this code: Imagick::setCompressionQuality
$im = new imagick(realpath($file).'[0]');
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->writeImage("imagename.jpg");
Alternative solution: this may help:
<?php
//Note that the function returns an Imagick object and does not modify the existing object. Below is my code for converting a PNG with transparency into a JPG with a background color. This code illustrates the difference.
$im = new Imagick($filename);
$im->setImageBackgroundColor('white');
$im->flattenImages(); // This does not do anything.
$im = $im->flattenImages(); // Use this instead.
$im->setImageFormat('jpg');
$im->writeImage('image.jpg');
?>
use like this
class ImageConvertorLib{
private $CI;
/**
* loading codeIgniter instance
*/
public function __construct(){
$this->CI =& get_instance();
}
public function pdfToJpg($param){
$filename = $param['filename'];
$image_name = $param['image_name'];
$path = $param['path'];
$db_path = $param['db_path'];
$im = new Imagick();
$im->setResolution(220,220);
$im->readimage($filename."[0]");
$im->setImageFormat('jpeg');
$im->setImageBackgroundColor('#ffffff');
$im->flattenImages();
$image_name = $image_name.".jpg";//"save_as_name.jpg";
$imageprops = $im->getImageGeometry();
/*if ($imageprops['width'] <= 175 && $imageprops['height'] <= 300) {
// don't upscale
} else {
$im->resizeImage(175,300, imagick::FILTER_LANCZOS, 0.9, true);
}*/
$im->writeImage($path.$image_name);
if($im){
$Img = array();
$Img['status'] = 1;
$Img['image'] = ($db_path.$image_name);
return $Img;
}
$im->clear();
$im->destroy();
}
}
After endless attempts to append a pdf file with a jpeg image without getting black areas, I found the solution: the function transformImageColorspace
Used in this order works perfectly:
$im = new Imagick();
$im->readImage("file.pdf");
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->setImageFormat('jpeg');
$im->writeImage('image.jpg');

Generate and return an image within Silverstripe Framework

I am trying to generate and return an image to the template using the Silverstripe 3.0 Framework and am getting some issues.
I return this to the browser in a variable like so:
public function make_image(){
$string = 'string';
$im = imagecreate(300,300);
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
}
The browser renders this as:
�PNG IHDR,,C�6PLTE��<��*�"IDATh���1 �Om ?�x-��{�IEND�B`�
The headers that are being returned are text/html and there is only one request that makes me think there is something strange going on there. Can anyone help me out with this.
Possibly a different way of achieving this is Silverstripe using the Image() class?
I'm not too sure why you need to play with your http headers. I understand you're going through intermediary steps to generate your image, so you need to see it being generated, but if your initial goal is to generate an image to include it in a template, this might help:
public function StringImage($string) {
$filePath = ASSETS_PATH.'/'.$string.'.png';
if (!file_exists(ASSETS_PATH.'/'.$string.'.png')){
$stringFontSize = 11;
$dimensions = imagettfbbox($stringFontSize, 90, 'Arial', $string);
$gd = new GD();
$width = $dimensions[2] - $dimensions[4];
$height = $dimensions[7] - $dimensions[5];
$image = imagecreatetruecolor($width, $height);
imagefilledrectangle($image, 0, 0, $width, $height, 0xFFFFFF);
imagettftext($image, $stringFontSize, 90, $width, $height, 0x000000, 'Arial', $string);
$gd->setGD($image);
$gd->writeTo($filePath);
}
return '<img src="'.ASSETS_DIR.'/'.$string.'.png'.'" alt="string"/>';
}

Categories