Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Created September 9, 2014 19:03
Show Gist options
  • Save khoand0000/5252b16f2e8a6a2c3359 to your computer and use it in GitHub Desktop.
Save khoand0000/5252b16f2e8a6a2c3359 to your computer and use it in GitHub Desktop.
Image class
<?php
/**
* Author: videde
* Date: 8/21/13
* Time: 1:27 AM
*/
class Image
{
public static function resizeImage($filename, $url, $newWidth, $newHeight, $square = false)
{
$img = self::do_curl($url);
$margin = 5;
if ($img != "") {
$extension = 'tmp';
file_put_contents(dirname(__FILE__) . '/logos/original_' . $filename . '.' . $extension, $img);
$extension = self::getImageExtension(dirname(__FILE__) . '/logos/original_' . $filename . '.' . $extension, false);
$path = dirname(__FILE__) . '/logos/original_' . $filename . '.' . $extension;
rename(dirname(__FILE__) . '/logos/original_' . $filename . '.tmp', $path);
switch ($extension) {
case 'png':
$source = imagecreatefrompng($path);
break;
case 'gif':
$source = imagecreatefromgif($path);
break;
default:
$source = imagecreatefromjpeg($path);
break;
}
// Get new sizes
// list($width, $height) = getimagesizefromstring($img); // PHP >= 5.4
list($width, $height) = getimagesize(dirname(__FILE__) . '/logos/original_' . $filename . '.' . $extension);
// Load
if ($square == false) {
$percent = sqrt(($newWidth * $newHeight * 1.0) / ($width * $height));
$finalWidth = $width * $percent;
$finalHeight = $height * $percent;
$resizedWidth = $finalWidth - ($margin * 2);
$resizedHeight = $resizedWidth * ($finalHeight * 1.0 / $finalWidth);
$thumb = imagecreatetruecolor($finalWidth, $finalHeight);
$x = $margin;
$y = ($finalHeight - $resizedHeight) / 2;
} else {
$finalHeight = $finalWidth = max($newWidth, $newHeight);
if ($width > $height) {
$resizedWidth = $finalWidth - ($margin * 2);
$resizedHeight = $resizedWidth * ($height * 1.0 / $width);
$x = $margin;
} else {
$resizedWidth = $finalWidth * ($width * 1.0 / $height);
if ($finalWidth - $resizedWidth < $margin * 2) {
$resizedWidth = $finalWidth - ($margin * 2);
$resizedHeight = $resizedWidth * ($height * 1.0 / $width);
} else {
$resizedHeight = $finalHeight;
}
$x = ($finalWidth - $resizedWidth) / 2;
}
$y = ($finalHeight - $resizedHeight) / 2;
$thumb = imagecreatetruecolor($finalWidth, $finalHeight);
}
$bg = imagecolorallocate($thumb, 255, 255, 255);
imagefilledrectangle($thumb, 0, 0, $finalWidth, $finalHeight, $bg);
// if ($extension == 'png' || $extension == 'gif') {
//// imagealphablending($thumb, false);
// }
// Resize
imagecopyresized($thumb, $source, $x, $y, 0, 0, $resizedWidth, $resizedHeight, $width, $height);
// add border
$border_color = imagecolorallocate($thumb, 0, 0, 0);
imagerectangle($thumb, 0, 0, $finalWidth - 1, $finalHeight - 1, $border_color);
// Output
self::saveImage($thumb, $extension, dirname(__FILE__) . '/logos/' . $filename . '.' . $extension);
imagedestroy($thumb);
imagedestroy($source);
echo "$url - $filename.$extension<br>";
return true;
} else {
echo "Can\'t save image at $url<br>";
return false;
}
}
public static function getImageExtension($filename, $includeDot = true, $shorterExtensions = true)
{
$image_info = @getimagesize($filename);
if (!$image_info || empty($image_info[2])) {
return false;
}
if (!function_exists('image_type_to_extension')) {
/**
* Given an image filename, get the file extension.
*
* @param $imagetype
* One of the IMAGETYPE_XXX constants.
* @param $include_dot
* Whether to prepend a dot to the extension or not. Default to TRUE.
* @param $shorterExtensions
* Whether to use a shorter extension or not. Default to TRUE.
* @return
* A string with the extension corresponding to the given image type, or
* FALSE on failure.
*/
function image_type_to_extensiona($imagetype, $include_dot = true)
{
// Note we do not use the IMAGETYPE_XXX constants as these will not be
// defined if GD is not enabled.
$extensions = array(
1 => 'gif',
2 => 'jpeg',
3 => 'png',
4 => 'swf',
5 => 'psd',
6 => 'bmp',
7 => 'tiff',
8 => 'tiff',
9 => 'jpc',
10 => 'jp2',
11 => 'jpf',
12 => 'jb2',
13 => 'swc',
14 => 'aiff',
15 => 'wbmp',
16 => 'xbm',
);
// We are expecting an integer between 1 and 16.
$imagetype = (int)$imagetype;
if (!$imagetype || !isset($extensions[$imagetype])) {
return false;
}
return ($include_dot ? '.' : '') . $extensions[$imagetype];
}
}
$extension = image_type_to_extension($image_info[2], $includeDot);
if (!$extension) {
return false;
}
if ($shorterExtensions) {
$replacements = array(
'jpeg' => 'jpg',
'tiff' => 'tif',
);
$extension = strtr($extension, $replacements);
}
return $extension;
}
public static function saveImage($img, $extension, $filename)
{
if ($extension == 'png' || $extension == 'gif') {
imagesavealpha($img, true);
if ($extension == 'png')
imagepng($img, $filename);
else
imagegif($img, $filename);
} else {
imagejpeg($img, $filename);
}
}
public static function hex2Rgb($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment