Created
March 23, 2018 01:38
-
-
Save vensires/affd4b6b014fa984fdc442ba8a21d78c to your computer and use it in GitHub Desktop.
The following script takes the path of an image and a color value to set to Alpha. Then the image takes as background the $bgcolor. A final parameter $mirror is used to mirror the image horizontally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* The callback function to process the image. | |
* | |
* @param string $filepath | |
* The file to the path. | |
* @param string $color | |
* The HEX value of the color. | |
* @param string $bgcolor | |
* The HEX value of the bgcolor. | |
* @param bool $mirror | |
* Set to TRUE to to mirror the image. Defaults to FALSE. | |
* | |
* @return string | |
* The PNG's image data. | |
*/ | |
function gdimagemanipulator_imageprocess($filepath, $color = '000000', $bgcolor = 'FFFFFF', $mirror = FALSE) { | |
$image = imagecreatefrompng($filepath); | |
$width = imagesx($image); | |
$height = imagesy($image); | |
// Foreground color. | |
$color = array( | |
'R' => hexdec(substr($color, 0, 2)), // Get the RED color for the front. | |
'G' => hexdec(substr($color, 2, 2)), // Get the GREEN color for the front. | |
'B' => hexdec(substr($color, 4, 2)), // Get the BLUE color for the front, | |
); | |
$fg = imagecolorallocatealpha($image, $color['R'], $color['G'], $color['B'], 0); | |
imagefilter($image, IMG_FILTER_GRAYSCALE); | |
imagefilter($image, IMG_FILTER_COLORIZE, $color['R'], $color['G'], $color['B'], 0); | |
/* | |
* The following commented part of code is use to provide the image with | |
* a white background color. But since we have multiple images, this is no longer | |
* needed. It is only needed right not to give a transparent background; | |
*/ | |
$bgcolor = array( | |
// Get the RED color for the bg. | |
'R' => hexdec(substr($bgcolor, 0, 2)), | |
// Get the GREEN color for the bg. | |
'G' => hexdec(substr($bgcolor, 2, 2)), | |
// Get the BLUE color for the bg. | |
'B' => hexdec(substr($bgcolor, 4, 2)), | |
); | |
$image2 = imagecreatetruecolor($width, $height); | |
// To allow proper background color you might have to comment out the | |
// following two lines [...] | |
imagealphablending($image2,false); | |
imagesavealpha($image2,true); | |
// [...] And uncomment the next two lines to allow proper bgcolor. | |
//$bg = imagecolorallocatealpha($image2, $bgcolor['R'], $bgcolor['G'], $bgcolor['B'], 0); //set bg | |
//imagefill($image2,0,0,$bg); | |
// <ix the two pictures. | |
imagecopyresampled($image2, $image, 0, 0, 0, 0, $width, $height, $width, $height); | |
// Check if we should mirror image. | |
if($mirror) { | |
_gdimagemanipulator_imageprocess_flip($image2); | |
} | |
/* | |
* Change the following code to match your return needs. | |
*/ | |
// Display processed image. | |
drupal_set_header('Content-Type: image/png'); | |
$output = imagepng($image2); | |
imagedestroy($image); | |
imagedestroy($image2); | |
return $output; | |
} | |
/* | |
* Function to flip an image with GD. | |
* ImageMagick has a built-in function but with GD, We have to create one | |
* and with all these dependenciesthat ImageMagick has, this is a no-go. | |
*/ | |
function _gdimagemanipulator_imageprocess_flip(&$image, $x = 0, $y = 0, $width = null, $height = null){ | |
if ($width < 1) { | |
$width = imagesx($image); | |
} | |
if ($height < 1) { | |
$height = imagesy($image); | |
} | |
// Truecolor provides better results, if possible. | |
if (function_exists('imageistruecolor') && imageistruecolor($image)) { | |
$tmp = imagecreatetruecolor(1, $height); | |
} | |
else { | |
$tmp = imagecreate(1, $height); | |
} | |
$x2 = $x + $width - 1; | |
for ($i = (int) floor(($width - 1) / 2); $i >= 0; $i--){ | |
// Backup right stripe. | |
imagecopy($tmp, $image, 0, 0, $x2 - $i, $y, 1, $height); | |
// Copy left stripe to the right. | |
imagecopy($image, $image, $x2 - $i, $y, $x + $i, $y, 1, $height); | |
// Copy backuped right stripe to the left. | |
imagecopy($image, $tmp, $x + $i, $y, 0, 0, 1, $height); | |
} | |
imagedestroy($tmp); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment