I have created two imagick objects $obj_one add $obj_two and loaded image from file test.pdf into $obj_one.
Now i need to load image from $obj_one into $obj_two pretty much the same as from the file, but from another imagick object.
How can i do it?
Note i set $obj_two resolution twice as much than $obj_one and i need image to resize upon loading to $obj_one.
EDIT:
$obj_one = new Imagick();
$obj_two = new Imagick();
$obj_one->setOption('pdf:use-cropbox', 'true');
$obj_one->readImage("docs/test.pdf");
$output_x_res = 800; //px
$output_y_res = $obj_one->getImageHeight() * $output_x_res / $obj_one->getImageWidth();
$Img_Dpi_Arr = $obj_one->getImageResolution();
$final_x_dpi = ceil(($Img_Dpi_Arr['x'] / $obj_one->getImageWidth()) * $output_x_res);
$final_y_dpi = ceil(($Img_Dpi_Arr['y'] / $obj_one->getImageHeight()) * $output_y_res);
$obj_two->setResolution($final_x_dpi,$final_y_dpi);
...
Related
private function processImageUpload($image,$image_name){
//using image png function to change any image from jpeg to png and store to public folder
$img = imagepng(imagecreatefromstring(file_get_contents($image)), config('app.LOGO_MEDIA_PATH').'/'.$image_name.'.png');
/*
* TODO : change the image replacing functionality
* from static name to upload to uploads folder
* and get URL form settings table
*/
$url = "/front/common/img/".$image_name.'.png';
return $url;
}
I enable the imagick extension in php.ini file but didn't happen anything
I got the solution..Just need to add case with the image type. In this we can't convert png to png.
private function processImageUpload($image,$image_name){
if($image->getMimeType() !== "image/png"){
//using image png function to change any image from jpeg to png and store to public folder
$img = imagepng(imagecreatefromstring(file_get_contents($image)), config('app.LOGO_MEDIA_PATH').'/'.$image_name.'.png');
}else{
$image->move(config('app.LOGO_MEDIA_PATH'), $image_name.'.png');
}
/*
* TODO : change the image replacing functionality
* from static name to upload to uploads folder
* and get URL form settings table
*/
$url = "/front/common/img/".$image_name.'.png';
return $url;
}
I have two function in two separate class, both have some Imagick processing.
I want the first Imagick to resize image and then send it to another Imagick instance without saving it as a file.
In the first class crop
function cropimage($inputfile){
$image = new Imagick($inputfile);
$image->cropimage($w, $h, $x, $y);
# and here I wish to return some $image->output???() which let me to use it in second class
return ????;
}
And in second class resize
function resizeImage(){
$crop = new crop(); // my first class instance
$image_to_work = $crop->cropimage($this->image); // output from first class
$image = new Imagick($image_to_work);
$image->resizeImage($width, $height);
$image->writeImage($outputfile);
}
Any ideas?
Thanks in advance.
Cheers
Images can be passed around as variables:
function cropimage($inputfile) {
$image = new Imagick($inputfile);
$image->cropimage($w, $h, $x, $y);
return $image;
}
function resizeImageAndSave($image) {
$image->resizeImage($width, $height);
$image->writeImage($outputfile);
}
$image = cropimage("./test.png"); // return the image
resizeImageAndSave($image); // pass it into the next function
Use sessions.
First, make sure you have session_start() in every script that will utilize sessions.
Then do something like $_SESSION['image'] = $image and then your next script can access that session variable.
I am trying to resize and replace an image uploaded by the user but the most I can do is resize and output as another file. I have used the library image magician to resize.
If someone can explain how I can do it without using the library it would be better.
public function add() {
$f=Base::instance()->get('FILES');
$fext=pathinfo ($f['usrimg']['name'],PATHINFO_EXTENSION);
$this->copyFrom('POST');
$this->save();
$newName=str_pad($this->_id,5,"0").'.'.$fext;
move_uploaded_file($f['usrimg']['tmp_name'], $newName);
$this->load('id='.$this->_id);
$this->set('photo',$newName);
$this->update();
require_once('php_image_magician.php');
$magicianObj = new imageLib($newName);
$magicianObj->resizeImage(100, 200);
$magicianObj->saveImage('q.jpg', 100);
}
If you want to use gd library, for example you can use this code for resizing jpeg images:
$image=file_get_contents('image.jpg');
$src=imagecreatefromstring($image);
$x=imagesx($src);
$y=imagesy($src);
$y1=100; //new width
$x1=intval($y1*$x/$y); //new height proprtional to new width
$dst=imagecreatetruecolor($x1,$y1);
imagecopyresized($dst,$src,0,0,0,0,$x1,$y1,$x,$y);
header("Content-Type: image/jpeg");
imagejpeg($dst);
I am using yii\imagine\Image extension and want to add a watermark to my image.
Here's my code:
$watermarkImage = '#webroot/../images/watermark.png';
$image = '#webroot/../slike/img-4.jpg';
Image::watermark($image, $watermarkImage);
After this code is executed, nothing happens. What am I missing here?
The ::watermark() function creates the new image but doesn't automatically saves it. The function returns a Imagine\Gd\Image object. This object can be used to save the new files.
$watermarkImage = '#webroot/../images/watermark.png';
$image = '#webroot/../slike/img-4.jpg';
// Store the Image object in a variable
$newImage = Image::watermark($image, $watermarkImage);
// Call the save function to write the file to the disk.
$newImage->save(Yii::getAlias('#webroot/../slike/img-4-watermark.jpg'));
I'm looking to center crop and image using Imagick PHP apis (not command line version of Imagick).
Essentially I want to do what is possible via command line, using API. Here is an example via command line:
http://www.imagemagick.org/Usage/crop/#crop_gravity
Here is what I'm doing (not working). It always crops the upper left corner of the source:
$this->imagickObj->setGravity(\Imagick::GRAVITY_CENTER);
$this->imagickObj->cropImage(300,250,0,0);
$this->imagickObj->setImagePage(0, 0, 0, 0);
Why is the setGravity not applying to the image before the crop? http://www.php.net/manual/en/function.imagick-setgravity.php says it should apply to the object (in this case the single image)...
Its too late for the original person who asked the question but for future visitors, correct solution is
bool Imagick::cropThumbnailImage ( int $width , int $height )
Sorry for late reply but I too stuck here just 30 mins ago and first google result redirected me here. Hope same will not happen with others.
Looks like there is not support, here is how I ended up doing it:
https://gist.github.com/1364489
The Imagemagick object's cropImage() method's 3rd and 4th argument are defining the upper-left corner of the crop. Either try passing those as null (and use the setGravity() method), or you may actually have to calculate where the crop is supposed to take place and pop those numbers into the cropImage() method (and don't bother with setGravity()).
For what it's worth, I have done a lot of coding around Imagemagick using PHP, and due to the horrible documentation of the Imagemagick extension, I resorted to making lots of nice'd command line calls.
I have created component to crop and resize images
here is the code (yii2)
Component uses imagine/imagine extension, install it before
<?php
namespace common\components;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use Imagine\Imagick\Image;
class ResizeComponent
{
/**
* Resize image
* #param string $source source image path
* #param string $destination destination image path
* #param int $width
* #param int $height
* #param int $quality Jpeg sampling quality (0-100, 80 is best for seo)
* #return boolean is picture cropped
*/
public static function resizeImage($source, $destination, $width, $height, $quality = 80)
{
if (file_exists($source) && is_file($source)) {
$imagine = new Imagine();
$size = new Box($width, $height);
$mode = ImageInterface::THUMBNAIL_INSET;
$resizeimg = $imagine->open($source)->thumbnail($size, $mode);
$sizeR = $resizeimg->getSize();
$widthR = $sizeR->getWidth();
$heightR = $sizeR->getHeight();
$preserve = $imagine->create($size);
$startX = $startY = 0;
if ($widthR < $width) {
$startX = ($width - $widthR) / 2;
}
if ($heightR < $height) {
$startY = ($height - $heightR) / 2;
}
$preserve->paste($resizeimg, new Point($startX, $startY))
->save($destination, array('jpeg_quality' => $quality));
return true;
} else {
return false;
}
}
/**
* Crop image
* #param string $source source image path
* #param string $destination destination image path
* #param int $width
* #param int $height
* #param int $quality Jpeg sampling quality (0-100, 80 is best for seo)
* #return boolean is picture cropped
*/
public static function cropImage($source, $destination, $width, $height, $quality = 80)
{
if (file_exists($source) && is_file($source)) {
$imagine = new Imagine();
$size = new Box($width, $height);
$mode = ImageInterface::THUMBNAIL_OUTBOUND;
$image = $imagine->open($source)->thumbnail($size, $mode);
$image->thumbnail($size, $mode)->save($destination, array('jpeg_quality' => $quality));
return true;
} else {
return false;
}
}
}
The difference between crop and resize is :
crop cant display all image, so borders will be cropped (best for not informative thumbnails)
resize displays full image, but borders will be filled with static color (or transperency if needed) (best if all image needed to be shown, as in shop catalog)
Use this component statically, best practice as ServiceLocator