ImageMagick equivalent to Box-shadow CSS property - php

I want to imitate the box-shadow CSS property in ImageMagick for an image. I want it to behave exactly the same. Is there any function or equivalent mapping?

<?php
/* Read the image into the object */
$im = new Imagick( 'a.jpg' );
$im->setImageFormat("png");
/* Make the image a little smaller, maintain aspect ratio */
$im->thumbnailImage( 200, null );
/* Clone the current object */
$shadow = $im->clone();
/* Set image background color to black (this is the color of the shadow) */
$shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
/* Create the shadow */
$shadow->shadowImage( 80, 3, 5, 5 );
/* Imagick::shadowImage only creates the shadow. That is why the original image is composited over it */
$shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );
/* Display the image */
header( "Content-Type: image/jpeg" );
echo $shadow;
?>
also you can see imagemagick for bash script

I think what you're looking for is Imagick::shadowImage (-shadow switch for CLI)

Related

How to remove background in images using intervention image

I'm trying to remove the white background in logos to get a transparent icon, for example
on this image
Is .jpg, I want to remove withe background (Only what's outside if is possible, otherwise removing all the white background)
I'm trying using laravel intervention image with this code
public function resizeImage($logo, $placeName, $domain)
{
$mask = Image::make($logo)
->contrast(100)
->contrast(50)
->trim('top-left', null, 40)
->invert(); // invert it to use as a mask
$new_image = Image::canvas($mask->width(), $mask->height(), '#000000')
->mask($mask)
->resize(32,32, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
->encode('png', 100);
$route = $domain . '/favicon/favicon.png';
Storage::disk('gcs')
->put($route, $new_image);
$route = Storage::disk('gcs')
->url($route);
return $route;
}
but this is my result
the image lost color (black colored)
what am I doing wrong? I feel that I am very close to achieving it, however, I need the icons to have color
$img = Image::canvas(800, 600);
Call canvas method without background color.
See more on http://image.intervention.io/api/canvas

How to use PHP drawing functions with Laravel

I'm trying to use PHP shape drawing functions with the Laravel framework but I am having some trouble.
So, I just to test it out, basically just copied the example from php.net and used it in a closure and a test route.
Route::get('maptest', function()
{
$image = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($image, 227, 32, 32);
imagefilledrectangle($image, 100, 80, 50, 25, $red);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
});
doing it this way works fine.
the problem is when I throw a Controller and a View into the mix. When I set the headers and what not and create a Response, I get a little broken picture icon in the top left corner of the window and nothing on the page loads at all.
this is how I am setting the headers.
(I know this is posted everywhere, but none of the solutions I could find seemed to work for me.)
public function show($id)
{
$user = User::find($id);
$view = View::make('profile/home', ['user' => $user]);
$response = Response::make($view, 200);
$response->header('Content-type', 'image/png');
return $response;
}
So the result of the above code (inside my controller) gives me a little broken picture icon in top left corner of window.

How to move the generated graph into desired folder in php?

I have generated the bar graph using php.I have downloaded class library from this link. http://www.pchart.net/download.I am getting the graph.
what i want to do is after creating the image,it should not output to the browser.Instead of that I want to save the images in particular folder. what should i do for that? Thanks in advance.This is my code
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pPie.class.php");
include("pChart/class/pImage.class.php");
$MyData = new pData();
$MyData->addPoints($val,"ScoreA");
$MyData->setSerieDescription("ScoreA","Application A");
/* Define the absissa serie */
$MyData->addPoints($keys,"Labels");
$MyData->setAbscissa("Labels");
/* Create the pChart object */
$myPicture = new pImage(300,260,$MyData);
/* Draw a solid background */
$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myPicture->drawFilledRectangle(0,0,300,300,$Settings);
/* Overlay with a gradient */
$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,300,260,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,300,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
/* Add a border to the picture */
$myPicture->drawRectangle(0,0,299,259,array("R"=>0,"G"=>0,"B"=>0));
/* Write the picture title */
$myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"pPie - Draw 2D pie charts",array("R"=>255,"G"=>255,"B"=>255));
/* Set the default font properties */
$myPicture->setFontProperties(array("FontName"=>"pChart/fonts/Forgotte.ttf","FontSize"=>10,"R"=>80,"G"=>80,"B"=>80));
/* Create the pPie object */
$PieChart = new pPie($myPicture,$MyData);
/* Draw an AA pie chart */
$PieChart->draw3DPie(160,140,array("Radius"=>70,"DrawLabels"=>TRUE,"LabelStacked"=>TRUE,"Border"=>TRUE));
/* Write the legend box */
$myPicture->setShadow(FALSE);
$PieChart->drawPieLegend(15,40,array("Alpha"=>20));
$myPicture->autoOutput("image");
Try to use:
$myPicture->render("image_name.png");
It worked for me in 1.x, don't know about 2.x - did not used it.
render() is documented in the pChart wiki on below link:
http://wiki.pchart.net/doc.faq.script.output.html

Imagick with MAMP + php 5.3.6 + cakephp

I'm having an issue with imagick running on a MAMP installation within a cakephp app.
I have followed the install instructions as indicated here and the install seems to have worked in the sense that the class 'Imagick' exists for me if i test it in a php script (I can see the module is loaded in phpinfo). However any examples that I run utilising the class hang as soon as I echo any content. My view is:
<?php
/* Create a new imagick object */
$im = new Imagick();
/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");
/* Create imagickdraw object */
$draw = new ImagickDraw();
/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);
/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);
/* Close the pattern */
$draw->popPattern();
/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');
/* Set font size to 52 */
$draw->setFontSize(52);
/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");
/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");
/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);
/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);
/* Set the format to PNG */
$canvas->setImageFormat('png');
/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>
This script will just hang as soon as the echo $canvas is encountered. The script works perfectly in a plain old php file i.e outside of cake but it hangs when visited via my cakephp app action. My action code is:
public function test(){
$this->layout = false;
}
Cake error log is empty.
Okay the issue was with how the header was set in cakephp. Cake didn't allow me to set the page header in the view that way.
I added the following code to the test action:
$this->response->type("image/png");
And it works perfectly now.

center crop with gravity using Imagick and PHP

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

Categories