Skip to content

Instantly share code, notes, and snippets.

@doulmi
Created March 9, 2017 08:56
Show Gist options
  • Save doulmi/73c22e3d0dd496acb367741b5d69159c to your computer and use it in GitHub Desktop.
Save doulmi/73c22e3d0dd496acb367741b5d69159c to your computer and use it in GitHub Desktop.
ImageUtils
<?php
namespace App;
use Exception;
class ImageUtils
{
public static $_JPG = "jpg";
public static $_PNG = "png";
public static $_GIF = "gif";
public static $_UNKNOWN = "unknown";
private $font;
private $img;
private $imgPath;
private $width;
private $height;
private $result;
private $filigrane;
private $productRatio;
private $format;
public function __construct($imgPath)
{
$this->font = dirname(__FILE__) . '/fonts/arial.ttf';
$this->productRatio = 3 / 4;
$this->imgPath = $imgPath;
$this->format = self::getRealFileType($imgPath);
if ($this->format == self::$_UNKNOWN) {
throw new Exception('Unknown format');
}
if (!file_exists($imgPath)) {
throw new Exception($imgPath . ' not found');
}
$this->img = $this->createImage($imgPath);
$this->width = imagesx($this->img);
$this->height = imagesy($this->img);
$this->result = dirname(__FILE__) . '/results/';
return $this;
}
public function setProductImageRatio($ratio)
{
$this->productRatio = $ratio;
}
public function setFont($font)
{
$this->font = $font;
}
/**
* return an png image with white background
* @param $imgPath
* @return resource
*/
private function whiteBgImage($imgPath)
{
$format = self::getRealFileType($imgPath);
switch ($format) {
case self::$_GIF :
return imagecreatefromgif($imgPath);
case self::$_JPG:
return imagecreatefromjpeg($imgPath);
case self::$_PNG:
$tmp = imagecreatefrompng($imgPath);
$newImg = imagecreatetruecolor($this->width, $this->height);
imagesavealpha($newImg, true);
$rgb = imagecolorallocatealpha($newImg, 255, 255, 255, 0);
imagefill($newImg, 0, 0, $rgb);
$color = imagecolorat($tmp, $this->width - 1, 1);
for ($x = 0; $x < $this->width; $x++) {
for ($y = 0; $y < $this->height; $y++) {
$c = imagecolorat($tmp, $x, $y);
if ($color != $c) {
imagesetpixel($newImg, $x, $y, $c);
}
}
}
imagedestroy($tmp);
return $newImg;
default:
assert("ERROR FORMAT");
}
}
/**
* Create an image: GIF, JPG, PNG supported
* @param $imgPath
* @return resource
*/
private function createImage($imgPath)
{
$format = self::getRealFileType($imgPath);
switch ($format) {
case self::$_GIF :
return imagecreatefromgif($imgPath);
case self::$_JPG:
return imagecreatefromjpeg($imgPath);
case self::$_PNG:
return imagecreatefrompng($imgPath);
default:
assert("ERROR FORMAT");
}
}
private function getRealSize($newWidth, $newHeight)
{
$width = $this->width;
$height = $this->height;
if ($width > $height) {
$realWidth = $newWidth * $this->productRatio;
$radio = $realWidth / $width;
$realHeight = $height * $radio;
$maxHeight = $newHeight * $this->productRatio;
$realHeight = $realHeight < $maxHeight ? $realHeight : $maxHeight;
} else {
$realHeight = $newHeight * $this->productRatio;
$radio = $realHeight / $height;
$realWidth = $width * $radio;
$maxWidth = $newWidth * $this->productRatio;
$realWidth = $realWidth < $maxWidth ? $realWidth : $maxWidth;
}
return [
'width' => $realWidth,
'height' => $realHeight
];
}
/**
* Resize image to ($newWidth, $newHeight)
* @param $newWidth
* @param $newHeight
* @return $this
*/
public function resize($newWidth, $newHeight)
{
$realSize = $this->getRealSize($newWidth, $newHeight);
$realWidth = $realSize['width'];
$realHeight = $realSize['height'];
$this->img = imagecreatetruecolor($realWidth, $realHeight);
$source = $this->whiteBgImage($this->imgPath);
imagecopyresized($this->img, $source, 0, 0, 0, 0, $realWidth, $realHeight, $this->width, $this->height);
$newBlackImg = imagecreatetruecolor($newWidth, $newHeight);
$bg = imagecolorallocate($newBlackImg, 255, 255, 255);
imagefilledrectangle($newBlackImg, 0, 0, $newWidth, $newHeight, $bg);
$marginLeft = ($newWidth - $realWidth) / 2;
$marginTop = ($newHeight - $realHeight) / 2;
imagecopy(
$newBlackImg,
$this->img,
$marginLeft,
$marginTop,
0, 0,
$realWidth,
$realHeight
);
$this->destroyImage();
$this->img = $newBlackImg;
$this->width = $newWidth;
$this->height = $newHeight;
return $this;
}
public function addFiligrane($filigranePath, $filigraneWidth, $filigraneHeight)
{
if ($filigranePath == '') return $this;
list($width, $height) = getimagesize($filigranePath);
$this->filigrane = $this->createImage($filigranePath);
imagecopy(
// destination
$this->img,
// source
$this->filigrane,
// destination x and y
($this->width - $width) / 2, ($this->height - $height) / 2,
// $this->width - 85, $this->height - 65,
// source x and y
0, 0,
// width and height of the area of the source to copy
$filigraneWidth, $filigraneHeight);
return $this;
}
/**
* function 16 hex color code to rgb
*/
public function hex2rgb($hexColor)
{
$color = str_replace('#', '', $hexColor);
if (strlen($color) > 3) {
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
} else {
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
'r' => hexdec($r),
'g' => hexdec($g),
'b' => hexdec($b)
);
}
return $rgb;
}
/**
* Save image to a path
* @return $this
*/
public function saveImage($path)
{
$this->drawImage($path);
return $this;
}
private function drawImage($path)
{
switch ($this->format) {
case self::$_GIF :
imagegif($this->img, $path);
break;
case self::$_JPG:
imagejpeg($this->img, $path, 100);
break;
case self::$_PNG:
imagepng($this->img, $path);
break;
default:
assert("ERROR FORMAT");
}
}
/**
* free memory
*/
public function destroyImage()
{
imagedestroy($this->img);
}
/**
* convert png to jpg
* @param $srcPath
* @param $destPath
*/
public static function png2Jpg($srcPath, $destPath)
{
$input = imagecreatefrompng($srcPath);
list($width, $height) = getimagesize($srcPath);
$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, $destPath);
imagedestroy($input);
imagedestroy($output);
}
public static function getRealFileType($filename)
{
if ($filename == '') return '';
$typeCode = exif_imagetype($filename);
switch ($typeCode) {
case IMAGETYPE_GIF: {
return 'gif';
}
case IMAGETYPE_JPEG: {
return 'jpg';
}
case IMAGETYPE_PNG: {
return 'png';
}
default: {
return 'jpg';
}
}
}
public static function isImage($img)
{
return @is_array(getimagesize($img));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment