I use GD library to upload / process photos, but when uploading a photo via Android or some mobile phone, photos are not uploaded correctly as an orientation. I have a site where users can upload photos from mobile, but all the photos that are uploaded from mobiles, show 90 degrees to the left when upload.
I now the problem is the exeif but i tried using a plugin but is not working. Any help is recommended.
Share my script code is:
CImageHandler.php
/**
* Image handler
* #author Yaroslav Pelesh aka Tokolist http://tokolist.com
* #link https://github.com/tokolist/yii-components
* #version 1.2
* #license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
ini_set('memory_limit', '8192M');
class CImageHandler extends CApplicationComponent
{
private $originalImage = null;
private $image = null;
private $format = 0;
private $width = 0;
private $height = 0;
private $mimeType = '';
private $fileName = '';
public $transparencyColor = [0, 0, 0];
const IMG_GIF = 1;
const IMG_JPEG = 2;
const IMG_PNG = 3;
const CORNER_LEFT_TOP = 1;
const CORNER_RIGHT_TOP = 2;
const CORNER_LEFT_BOTTOM = 3;
const CORNER_RIGHT_BOTTOM = 4;
const CORNER_CENTER = 5;
const CORNER_CENTER_TOP = 6;
const CORNER_CENTER_BOTTOM = 7;
const CORNER_LEFT_CENTER = 8;
const CORNER_RIGHT_CENTER = 9;
const FLIP_HORIZONTAL = 1;
const FLIP_VERTICAL = 2;
const FLIP_BOTH = 3;
public function getImage()
{
return $this->image;
}
public function getFormat()
{
return $this->format;
}
public function getWidth()
{
return $this->width;
}
public function getHeight()
{
return $this->height;
}
public function getMimeType()
{
return $this->mimeType;
}
public function __destruct()
{
$this->freeImage();
}
private function freeImage()
{
if (is_resource($this->image)) {
imagedestroy($this->image);
}
if ($this->originalImage !== null) {
if (is_resource($this->originalImage['image'])) {
imagedestroy($this->originalImage['image']);
}
$this->originalImage = null;
}
}
private function checkLoaded()
{
if (!is_resource($this->image)) {
throw new Exception('Load image first');
}
}
private function loadImage($file)
{
$result = [];
if ($imageInfo = #getimagesize($file)) {
$result['width'] = $imageInfo[0];
$result['height'] = $imageInfo[1];
$result['mimeType'] = $imageInfo['mime'];
switch ($result['format'] = $imageInfo[2]) {
case self::IMG_GIF:
if(file_exists($file)) {
$gd = #imagecreatefromstring(file_get_contents($file));
if ($gd === false) {
throw new Exception ('Invalid image gif format');
} else {
if ($result['image'] = imagecreatefromgif($file)) {
return $result;
} else {
throw new Exception('Invalid image gif format');
}
}
} else {
throw new Exception('Invalid image gif format');
}
break;
case self::IMG_JPEG:
if(file_exists($file)) {
$gd = #imagecreatefromstring(file_get_contents($file));
if ($gd === false) {
throw new Exception ('Invalid image jpeg format');
} else {
/*
//Read the JPEG image Exif data to get the Orientation value
if (function_exists('exif_read_data')) {
$exif = exif_read_data($file);
$orientation = #$exif['IFD0']['Orientation'];
if (!$orientation) {
$orientation = #$exif['Orientation'];
}
if(!$orientation) return;
$source = #imagecreatefromjpeg($file);
if(!$source) return;
switch ($orientation) {
case 1: // nothing
break;
case 2: // horizontal flip
imageflip($source, IMG_FLIP_HORIZONTAL);
break;
case 3: // 180 rotate left
$modifiedImage = imagerotate($source, 180, 0);
imagejpeg($modifiedImage, $file, 90); //save output to file system at full quality
break;
case 4: // vertical flip
imageflip($source, IMG_FLIP_VERTICAL);
break;
case 5: // vertical flip + 90 rotate right
imageflip($source, IMG_FLIP_VERTICAL);
$modifiedImage = imagerotate($source, -90, 0);
imagejpeg($modifiedImage, $file, 90); //save output to file system at full quality
break;
case 6: // 90 rotate right
$modifiedImage = imagerotate($source, -90, 0);
imagejpeg($modifiedImage, $file, 90); //save output to file system at full quality
break;
case 7: // horizontal flip + 90 rotate right
imageflip($source, IMG_FLIP_HORIZONTAL);
$modifiedImage = imagerotate($source, -90, 0);
imagejpeg($modifiedImage, $file, 90); //save output to file system at full quality
break;
case 8: // 90 rotate left
$modifiedImage = imagerotate($source, 90, 0);
imagejpeg($modifiedImage, $file, 90); //save output to file system at full quality
break;
}
}
//Read the JPEG image Exif data to get the Orientation value
*/
if ($result['image'] = #imagecreatefromjpeg($file)) {
return $result;
} else {
throw new Exception('Invalid image jpeg format');
}
}
} else {
throw new Exception('Invalid image jpeg format');
}
break;
case self::IMG_PNG:
if(file_exists($file)) {
$gd = #imagecreatefromstring(file_get_contents($file));
if ($gd === false) {
throw new Exception ('Invalid image png format');
} else {
if ($result['image'] = imagecreatefrompng($file)) {
return $result;
} else {
throw new Exception('Invalid image png format');
}
}
} else {
throw new Exception('Invalid image png format');
}
break;
default:
throw new Exception('Not supported image format');
}
} else {
throw new Exception('Invalid image file');
}
}
protected function initImage($image = false)
{
if ($image === false) {
$image = $this->originalImage;
}
$this->width = $image['width'];
$this->height = $image['height'];
$this->mimeType = $image['mimeType'];
$this->format = $image['format'];
//Image
if (is_resource($this->image))
imagedestroy($this->image);
//10-08-2018
$this->image = imagecreatetruecolor($this->width, $this->height);
$this->preserveTransparency($this->image);
imagecopy($this->image, $image['image'], 0, 0, 0, 0, $this->width, $this->height);
}
/**
* #param $file
*
* #return CImageHandler
* #throws Exception
*/
public function load($file)
{
$this->freeImage();
if (($this->originalImage = $this->loadImage($file))) {
$this->initImage();
$this->fileName = $file;
return $this;
} else {
return false;
}
}
public function reload()
{
$this->checkLoaded();
$this->initImage();
return $this;
}
private function preserveTransparency($newImage)
{
switch ($this->format) {
case self::IMG_GIF:
$color = imagecolorallocate(
$newImage,
$this->transparencyColor[0],
$this->transparencyColor[1],
$this->transparencyColor[2]
);
imagecolortransparent($newImage, $color);
imagetruecolortopalette($newImage, false, 256);
break;
case self::IMG_PNG:
imagealphablending($newImage, false);
$color = imagecolorallocatealpha(
$newImage,
$this->transparencyColor[0],
$this->transparencyColor[1],
$this->transparencyColor[2],
0
);
imagefill($newImage, 0, 0, $color);
imagesavealpha($newImage, true);
break;
}
}
public function resize($toWidth, $toHeight, $proportional = true)
{
$this->checkLoaded();
$toWidth = $toWidth !== false ? $toWidth : $this->width;
$toHeight = $toHeight !== false ? $toHeight : $this->height;
if ($proportional) {
$newHeight = $toHeight;
$newWidth = round($newHeight / $this->height * $this->width);
if ($newWidth > $toWidth) {
$newWidth = $toWidth;
$newHeight = round($newWidth / $this->width * $this->height);
}
} else {
$newWidth = $toWidth;
$newHeight = $toHeight;
}
$newImage = imagecreatetruecolor($newWidth, $newHeight);
$this->preserveTransparency($newImage);
imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
imagedestroy($this->image);
$this->image = $newImage;
$this->width = $newWidth;
$this->height = $newHeight;
return $this;
}
public function thumb($toWidth, $toHeight, $proportional = true)
{
$this->checkLoaded();
if ($toWidth !== false)
$toWidth = min($toWidth, $this->width);
if ($toHeight !== false)
$toHeight = min($toHeight, $this->height);
$this->resize($toWidth, $toHeight, $proportional);
return $this;
}
public function watermark($watermarkFile, $offsetX, $offsetY, $corner = self::CORNER_RIGHT_BOTTOM, $zoom = false)
{
$this->checkLoaded();
if ($wImg = $this->loadImage($watermarkFile)) {
$posX = 0;
$posY = 0;
$watermarkWidth = $wImg['width'];
$watermarkHeight = $wImg['height'];
if ($zoom !== false) {
$dimension = round(max($this->width, $this->height) * $zoom);
$watermarkHeight = $dimension;
$watermarkWidth = round($watermarkHeight / $wImg['height'] * $wImg['width']);
if ($watermarkWidth > $dimension) {
$watermarkWidth = $dimension;
$watermarkHeight = round($watermarkWidth / $wImg['width'] * $wImg['height']);
}
}
switch ($corner) {
case self::CORNER_LEFT_TOP:
$posX = $offsetX;
$posY = $offsetY;
break;
case self::CORNER_RIGHT_TOP:
$posX = $this->width - $watermarkWidth - $offsetX;
$posY = $offsetY;
break;
case self::CORNER_LEFT_BOTTOM:
$posX = $offsetX;
$posY = $this->height - $watermarkHeight - $offsetY;
break;
case self::CORNER_RIGHT_BOTTOM:
$posX = $this->width - $watermarkWidth - $offsetX;
$posY = $this->height - $watermarkHeight - $offsetY;
break;
case self::CORNER_CENTER:
$posX = floor(($this->width - $watermarkWidth) / 2);
$posY = floor(($this->height - $watermarkHeight) / 2);
break;
case self::CORNER_CENTER_TOP:
$posX = floor(($this->width - $watermarkWidth) / 2);
$posY = $offsetY;
break;
case self::CORNER_CENTER_BOTTOM:
$posX = floor(($this->width - $watermarkWidth) / 2);
$posY = $this->height - $watermarkHeight - $offsetY;
break;
case self::CORNER_LEFT_CENTER:
$posX = $offsetX;
$posY = floor(($this->height - $watermarkHeight) / 2);
break;
case self::CORNER_RIGHT_CENTER:
$posX = $this->width - $watermarkWidth - $offsetX;
$posY = floor(($this->height - $watermarkHeight) / 2);
break;
default:
throw new Exception('Invalid $corner value');
}
imagecopyresampled(
$this->image,
$wImg['image'],
$posX,
$posY,
0,
0,
$watermarkWidth,
$watermarkHeight,
$wImg['width'],
$wImg['height']
);
imagedestroy($wImg['image']);
return $this;
} else {
return false;
}
}
public function flip($mode)
{
$this->checkLoaded();
$srcX = 0;
$srcY = 0;
$srcWidth = $this->width;
$srcHeight = $this->height;
switch ($mode) {
case self::FLIP_HORIZONTAL:
$srcX = $this->width - 1;
$srcWidth = -$this->width;
break;
case self::FLIP_VERTICAL:
$srcY = $this->height - 1;
$srcHeight = -$this->height;
break;
case self::FLIP_BOTH:
$srcX = $this->width - 1;
$srcY = $this->height - 1;
$srcWidth = -$this->width;
$srcHeight = -$this->height;
break;
default:
throw new Exception('Invalid $mode value');
}
$newImage = imagecreatetruecolor($this->width, $this->height);
$this->preserveTransparency($newImage);
imagecopyresampled($newImage, $this->image, 0, 0, $srcX, $srcY, $this->width, $this->height, $srcWidth, $srcHeight);
imagedestroy($this->image);
$this->image = $newImage;
//dimensions not changed
return $this;
}
public function rotate($degrees)
{
$this->checkLoaded();
$degrees = (int)$degrees;
$this->image = imagerotate($this->image, $degrees, 0);
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
return $this;
}
public function crop($width, $height, $startX = false, $startY = false)
{
$this->checkLoaded();
$width = (int)$width;
$height = (int)$height;
//Centered crop
$startX = $startX === false ? floor(($this->width - $width) / 2) : intval($startX);
$startY = $startY === false ? floor(($this->height - $height) / 2) : intval($startY);
//Check dimensions
$startX = max(0, min($this->width, $startX));
$startY = max(0, min($this->height, $startY));
$width = min($width, $this->width - $startX);
$height = min($height, $this->height - $startY);
$newImage = imagecreatetruecolor($width, $height);
$this->preserveTransparency($newImage);
imagecopyresampled($newImage, $this->image, 0, 0, $startX, $startY, $width, $height, $width, $height);
imagedestroy($this->image);
$this->image = $newImage;
$this->width = $width;
$this->height = $height;
return $this;
}
public function text($text, $fontFile, $size = 12, $color = [0, 0, 0],
$corner = self::CORNER_LEFT_TOP, $offsetX = 0, $offsetY = 0, $angle = 0, $alpha = 0)
{
$this->checkLoaded();
$bBox = imagettfbbox($size, $angle, $fontFile, $text);
$textHeight = $bBox[1] - $bBox[7];
$textWidth = $bBox[2] - $bBox[0];
switch ($corner) {
case self::CORNER_LEFT_TOP:
$posX = $offsetX;
$posY = $offsetY;
break;
case self::CORNER_RIGHT_TOP:
$posX = $this->width - $textWidth - $offsetX;
$posY = $offsetY;
break;
case self::CORNER_LEFT_BOTTOM:
$posX = $offsetX;
$posY = $this->height - $textHeight - $offsetY;
break;
case self::CORNER_RIGHT_BOTTOM:
$posX = $this->width - $textWidth - $offsetX;
$posY = $this->height - $textHeight - $offsetY;
break;
case self::CORNER_CENTER:
$posX = floor(($this->width - $textWidth) / 2);
$posY = floor(($this->height - $textHeight) / 2);
break;
case self::CORNER_CENTER_TOP:
$posX = floor(($this->width - $textWidth) / 2);
$posY = $offsetY;
break;
case self::CORNER_CENTER_BOTTOM:
$posX = floor(($this->width - $textWidth) / 2);
$posY = $this->height - $textHeight - $offsetY;
break;
case self::CORNER_LEFT_CENTER:
$posX = $offsetX;
$posY = floor(($this->height - $textHeight) / 2);
break;
case self::CORNER_RIGHT_CENTER:
$posX = $this->width - $textWidth - $offsetX;
$posY = floor(($this->height - $textHeight) / 2);
break;
default:
throw new Exception('Invalid $corner value');
}
if ($alpha > 0) {
$color = imagecolorallocatealpha($this->image, $color[0], $color[1], $color[2], $alpha);
} else {
$color = imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
}
imagettftext($this->image, $size, $angle, $posX, $posY + $textHeight, $color, $fontFile, $text);
return $this;
}
public function adaptiveThumb($width, $height)
{
$this->checkLoaded();
$width = intval($width);
$height = intval($height);
$widthProportion = $width / $this->width;
$heightProportion = $height / $this->height;
if ($widthProportion > $heightProportion) {
$newWidth = $width;
$newHeight = round($newWidth / $this->width * $this->height);
} else {
$newHeight = $height;
$newWidth = round($newHeight / $this->height * $this->width);
}
$this->resize($newWidth, $newHeight);
$this->crop($width, $height);
return $this;
}
public function resizeCanvas($toWidth, $toHeight, $backgroundColor = [255, 255, 255])
{
$this->checkLoaded();
$newWidth = min($toWidth, $this->width);
$newHeight = min($toHeight, $this->height);
$widthProportion = $newWidth / $this->width;
$heightProportion = $newHeight / $this->height;
if ($widthProportion < $heightProportion) {
$newHeight = round($widthProportion * $this->height);
} else {
$newWidth = round($heightProportion * $this->width);
}
$posX = floor(($toWidth - $newWidth) / 2);
$posY = floor(($toHeight - $newHeight) / 2);
$newImage = imagecreatetruecolor($toWidth, $toHeight);
$backgroundColor = imagecolorallocate($newImage, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]);
imagefill($newImage, 0, 0, $backgroundColor);
imagecopyresampled($newImage, $this->image, $posX, $posY, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
imagedestroy($this->image);
$this->image = $newImage;
$this->width = $toWidth;
$this->height = $toHeight;
return $this;
}
public function grayscale()
{
$newImage = imagecreatetruecolor($this->width, $this->height);
imagecopy($newImage, $this->image, 0, 0, 0, 0, $this->width, $this->height);
imagecopymergegray($newImage, $newImage, 0, 0, 0, 0, $this->width, $this->height, 0);
imagedestroy($this->image);
$this->image = $newImage;
return $this;
}
public function show($inFormat = false, $jpegQuality = 75)
{
$this->checkLoaded();
if (!$inFormat) {
$inFormat = $this->format;
}
switch ($inFormat) {
case self::IMG_GIF:
header('Content-type: image/gif');
imagegif($this->image);
break;
case self::IMG_JPEG:
header('Content-type: image/jpeg');
imagejpeg($this->image, null, $jpegQuality);
break;
case self::IMG_PNG:
header('Content-type: image/png');
imagepng($this->image);
break;
default:
throw new Exception('Invalid image format for putput');
}
return $this;
}
public function save($file = false, $toFormat = false, $jpegQuality = 75, $touch = false)
{
if (empty($file)) {
$file = $this->fileName;
}
$this->checkLoaded();
if (!$toFormat) {
$toFormat = $this->format;
}
switch ($toFormat) {
case self::IMG_GIF:
if (!imagegif($this->image, $file)) {
throw new Exception('Can\'t save gif file');
}
break;
case self::IMG_JPEG:
if (!imagejpeg($this->image, $file, $jpegQuality)) {
throw new Exception('Can\'t save jpeg file');
}
break;
case self::IMG_PNG:
if (!imagepng($this->image, $file)) {
throw new Exception('Can\'t save png file');
}
break;
default:
throw new Exception('Invalid image format for save');
}
if ($touch && $file != $this->fileName) {
touch($file, filemtime($this->fileName));
}
return $this;
}
}
Thank you #Yanik Lupien your simple code is very nice work :)
if (function_exists('exif_read_data')) {
$buffer = imagecreatefromjpeg($file);
$exif = #exif_read_data($file);
if ($exif && !empty($exif['Orientation']))
{
switch($exif['Orientation']) {
case 8:
$buffer = imagerotate($buffer, 90, 0);
break;
case 3:
$buffer = imagerotate($buffer, 180, 0);
break;
case 6:
$buffer = imagerotate($buffer, -90, 0);
break;
case 5: // vertical flip + 90 rotate right
$buffer = imagerotate($buffer, -90, 0);
break;
case 7: // horizontal flip + 90 rotate right
$buffer = imagerotate($buffer, -90, 0);
break;
}
}
imagejpeg($buffer, $file, 90);
}
Here is a small snippet to fix rotation.
$imagePathName = 'file.jpg';
$buffer = ImageCreateFromJPEG($imagePathName);
$exif = #exif_read_data($imagePathName);
if ($exif && !empty($exif['Orientation']))
{
switch($exif['Orientation']) {
case 8:
$buffer = imagerotate($buffer, 90, 0);
break;
case 3:
$buffer = imagerotate($buffer, 180, 0);
break;
case 6:
$buffer = imagerotate($buffer, -90, 0);
break;
}
}
imagejpeg($buffer, $imagePathName, 90);
Related
I am using a function from this question
PHP - Create Thumbnail & maintaining aspect ratio
The code is as below,
define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);
function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
{
list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
switch ($source_image_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
if ($source_gd_image === false) {
return false;
}
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
} elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
$thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
}
$thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
$img_disp = imagecreatetruecolor(THUMBNAIL_IMAGE_MAX_WIDTH,THUMBNAIL_IMAGE_MAX_HEIGHT);
$backcolor = imagecolorallocate($img_disp,0,0,0);
imagefill($img_disp,0,0,$backcolor);
imagecopy($img_disp, $thumbnail_gd_image, (imagesx($img_disp)/2)-(imagesx($thumbnail_gd_image)/2), (imagesy($img_disp)/2)-(imagesy($thumbnail_gd_image)/2), 0, 0, imagesx($thumbnail_gd_image), imagesy($thumbnail_gd_image));
imagejpeg($img_disp, $thumbnail_image_path, 90);
imagedestroy($source_gd_image);
imagedestroy($thumbnail_gd_image);
imagedestroy($img_disp);
return true;
}
It works well and will create thumbnail like this
I notice that the function will fill unused portion with black color
imagefill($img_disp,0,0,$backcolor);
How could I remove the black portion instead? I don't need fix width or height.
Maybe the easiest way is to preserve the aspect ratio.
// RESIZE AN IMAGE PROPORTIONALLY AND CROP TO THE CENTER
function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality=75)
{
// ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
$original = imagecreatefromjpeg($original_image_url);
if (!$original) return FALSE;
// GET ORIGINAL IMAGE DIMENSIONS
list($original_w, $original_h) = getimagesize($original_image_url);
// RESIZE IMAGE AND PRESERVE PROPORTIONS
$thumb_w_resize = $thumb_w;
$thumb_h_resize = $thumb_h;
if ($original_w > $original_h)
{
$thumb_h_ratio = $thumb_h / $original_h;
$thumb_w_resize = (int)round($original_w * $thumb_h_ratio);
}
else
{
$thumb_w_ratio = $thumb_w / $original_w;
$thumb_h_resize = (int)round($original_h * $thumb_w_ratio);
}
if ($thumb_w_resize < $thumb_w)
{
$thumb_h_ratio = $thumb_w / $thumb_w_resize;
$thumb_h_resize = (int)round($thumb_h * $thumb_h_ratio);
$thumb_w_resize = $thumb_w;
}
// CREATE THE PROPORTIONAL IMAGE RESOURCE
$thumb = imagecreatetruecolor($thumb_w_resize, $thumb_h_resize);
if (!imagecopyresampled($thumb, $original, 0,0,0,0, $thumb_w_resize, $thumb_h_resize, $original_w, $original_h)) return FALSE;
// ACTIVATE THIS TO STORE THE INTERMEDIATE IMAGE
// imagejpeg($thumb, 'thumbs/temp_' . $thumb_w_resize . 'x' . $thumb_h_resize . '.jpg', 100);
// CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
$final = imagecreatetruecolor($thumb_w, $thumb_h);
$thumb_w_offset = 0;
$thumb_h_offset = 0;
if ($thumb_w < $thumb_w_resize)
{
$thumb_w_offset = (int)round(($thumb_w_resize - $thumb_w) / 2);
}
else
{
$thumb_h_offset = (int)round(($thumb_h_resize - $thumb_h) / 2);
}
if (!imagecopy($final, $thumb, 0,0, $thumb_w_offset, $thumb_h_offset, $thumb_w_resize, $thumb_h_resize)) return FALSE;
// STORE THE FINAL IMAGE - WILL OVERWRITE $thumb_image_url
if (!imagejpeg($final, $thumb_image_url, $quality)) return FALSE;
return TRUE;
}
// USE CASE
echo '<a target="_blank" href="images/image_600x374.jpg">Original 600x374</a><br/>';
resize_and_crop('images/image_600x374.jpg', 'thumbs/temp_100x100.jpg', 100, 100);
echo '<a target="_blank" href="thumbs/temp_100x100.jpg">100x100</a><br/>';
resize_and_crop('images/image_600x374.jpg', 'thumbs/temp_200x100.jpg', 200, 100);
echo '<a target="_blank" href="thumbs/temp_200x100.jpg">200x100</a><br/>';
resize_and_crop('images/image_600x374.jpg', 'thumbs/temp_200x300.jpg', 200, 300);
echo '<a target="_blank" href="thumbs/temp_200x300.jpg">200x300</a><br/>';
How can I set this code to return images in 1:1 (square)?
The purpose is to create a square (non stretched) thumbnail.
I've tried making changes in the 'if section'. I get a square image but stretched. I want it to be cropped.
define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);
$source_image_path = {here the source filename};
$thumbnail_image_path = {here de thumb filename};
list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
switch ($source_image_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
} elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
$thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
}
$thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
PS. It's not a duplicate, I've read several questions of this topic, but I'm unable to integrate it whit my code.
This function did the trick
function crop_img($imgSrc){
//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);
//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail
$thumbSize = min($width,$height);
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
unlink($imgSrc);
imagejpeg($thumb,$imgSrc);
#imagedestroy($myImage);
#imagedestroy($thumb);
}
Found in: PHP crop image to fix width and height without losing dimension ratio
Use this code this code uploads image to folder and renames the file and thumb will be created with same name
HTML
<INPUT NAME="userfile[]" TYPE="file">
image directory "upimg/"
thumb directory thimg
php processing
$rename = md5(rand() * time());
$add = "upimg/" . $rename . $_FILES['userfile']['name'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $add)) {
echo "Successfully uploaded the image";
chmod("$add", 0777);
} else {
exit;
}
$n_width = 200;
$n_height = 200;
$tsrc = "thimg/" . $rename . $_FILES['userfile']['name'];
if (!($_FILES['userfile']['type'] == "image/jpeg" OR $_FILES['userfile']['type'] == "image/gif")) {
exit;
}
if ($_FILES['userfile']['type'] == "image/gif") {
$im = ImageCreateFromGIF($add);
$width = ImageSx($im);
$height = ImageSy($im);
$newimage = imagecreatetruecolor($n_width, $n_height);
imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage, $tsrc);
} elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage, $tsrc);
}
chmod("$tsrc", 0777);
}
if ($_FILES['userfile']['type'] == "image/jpeg") {
$im = ImageCreateFromJPEG($add);
$width = ImageSx($im);
$height = ImageSy($im);
$newimage = imagecreatetruecolor($n_width, $n_height);
imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
ImageJpeg($newimage, $tsrc);
chmod("$tsrc", 0777);
}
Code below resizes the images but I have a new requirement to it.
Requirement: I want to add padding to short edge with any colour. So when it is scaled down to 100(w)X150(h), it should be saved as 150X150 afted being padded.
For the padding option I've seen these two posts but failed to implement. Please help me to modify my code.
Resize/crop/pad a picture to a fixed size
Resize an image and fill gaps of proportions with a color
Thanks in advance
$this->defaults['width'] = 100;
$this->defaults['height'] = 150;
private function createThumbnail($sourceImage, $targetImage)
{
list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);
switch ($sourceType)
{
case IMAGETYPE_GIF:
$sourceGdImage = imagecreatefromgif($sourceImage);
break;
case IMAGETYPE_JPEG:
$sourceGdImage = imagecreatefromjpeg($sourceImage);
break;
case IMAGETYPE_PNG:
$sourceGdImage = imagecreatefrompng($sourceImage);
break;
}
if ($sourceGdImage === false)
{
return false;
}
$sourceAspectRatio = ($sourceWidth / $sourceHeight);
$thumbnailAspectRatio = ($this->defaults['width'] / $this->defaults['height']);
if ($sourceWidth <= $this->defaults['width'] && $sourceHeight <= $this->defaults['height'])
{
$thumbnailWidth = $sourceWidth;
$thumbnailHeight = $sourceHeight;
}
elseif ($thumbnailAspectRatio > $sourceAspectRatio)
{
$thumbnailWidth = (int) ($this->defaults['height'] * $sourceAspectRatio);
$thumbnailHeight = $this->defaults['height'];
}
else
{
$thumbnailWidth = $this->defaults['width'];
$thumbnailHeight = (int) ($this->defaults['width'] / $sourceAspectRatio);
}
$thumbnailGdImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresampled($thumbnailGdImage, $sourceGdImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
switch ($sourceType)
{
case IMAGETYPE_GIF:
imagegif($thumbnailGdImage, $targetImage, 90);
break;
case IMAGETYPE_JPEG:
imagejpeg($thumbnailGdImage, $targetImage, 90);
break;
case IMAGETYPE_PNG:
imagepng($thumbnailGdImage, $targetImage, 9);
break;
}
imagedestroy($sourceGdImage);
imagedestroy($thumbnailGdImage);
return true;
}
SOLUTION:
thumbnailDefaults:
height: 200
width: 200
red: 200
green: 0
blue: 0
private function createThumbnailWithPadding($sourceImage, $targetImage)
{
list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);
$sourceGdImage = imagecreatefromstring(file_get_contents($sourceImage));
//Determine scale based on the longest edge
if ($sourceHeight > $sourceWidth)
{
$scale = ($this->thumbnailDefaults['height'] / $sourceHeight);
}
else
{
$scale = ($this->thumbnailDefaults['width'] / $sourceWidth);
}
//Calculate new image dimensions
$thumbnailWidth = ($sourceWidth * $scale);
$thumbnailHeight = ($sourceHeight * $scale);
//Determine offset coordinates so that new image is centered
$offsetX = (($this->thumbnailDefaults['width'] - $thumbnailWidth) / 2);
$offsetY = (($this->thumbnailDefaults['height'] - $thumbnailHeight) / 2);
//Create new image and fill with background colour
$thumbnailGdImage = imagecreatetruecolor($this->thumbnailDefaults['width'], $this->thumbnailDefaults['height']);
//Set background colour
$bgColor = imagecolorallocate(
$thumbnailGdImage,
$this->thumbnailDefaults['red'],
$this->thumbnailDefaults['green'],
$this->thumbnailDefaults['blue']
);
//Fill background colour
imagefill($thumbnailGdImage, 0, 0, $bgColor);
//Copy and resize original image into center of new image
imagecopyresampled($thumbnailGdImage, $sourceGdImage, $offsetX, $offsetY, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
//clearstatcache();
switch ($sourceType)
{
case IMAGETYPE_GIF:
imagegif($thumbnailGdImage, $targetImage, 90);
break;
case IMAGETYPE_JPEG:
imagejpeg($thumbnailGdImage, $targetImage, 90);
break;
case IMAGETYPE_PNG:
imagepng($thumbnailGdImage, $targetImage, 9);
break;
}
imagedestroy($sourceGdImage);
imagedestroy($thumbnailGdImage);
return true;
}
I am currently resizing an image to a custom with keeping aspect ratio:
class ImgResizer {
var $originalFile = '$newName';
function ImgResizer($originalFile = '$newName') {
$this -> originalFile = $originalFile;
}
function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 95);
}
}
Usage:
$work = new ImgResizer($path);
$work -> resize(200, $path);
But i would like to get a 200x200px version o the image. And it should be vertically amd horizontally centered ( basically get the main 200px of the image)
is that possible?
-EDIT-
function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = $newWidth;
if ($width > $newWidth){
$srcx = $width/2 - $newWidth/2;
$destx = 0;
}
else{
$srcx = 0;
$destx = $newWidth/2 - $width/2;
}
if ($height > $newHeight){
$srcy = $height/2 - $newHeight/2;
$desty = 0;
}
else{
$srcy = 0;
$desty = $newHeight/2 - $height/2;
}
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, $destx, $desty, $srcx, $srcy, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 95);
}
Would create something unexpected: HTTP://209.51.221.243/integracion/files/uploads/1_050.JPG
Try
if ($width > $newWidth){
$srcx = $width/2 - $newWidth/2;
$destx = 0;
$w = $newWidth;
}
else{
$srcx = 0;
$destx = $newWidth/2 - $width/2;
$w = $width;
}
if ($height > $newHeight){
$srcy = $height/2 - $newHeight/2;
$desty = 0;
$h = $newHeight;
}
else{
$srcy = 0;
$desty = $newHeight/2 - $height/2;
$h = $keight;
}
imagecopyresampled($tmp, $src, $destx, $desty, $srcx, $srcy, $w, $h, $w, $h);
On upload of images, the code creates several different thumbnails. In some cases, the thumbnail may be larger than the original image, in which case a padding is applied. In the case of JPEGs, the padding is white, and all is well. In the case of GIFs and PNGs, the padding should be transparent. And it is. Or not. Which is weird.
If I was getting an entirely black or transparent padding, I'd have some idea where the problem is, but I'm getting paddings which are transparent in places and black in others, which makes no sense.
Sample PNG and GIF images at http://filedump.xn--es-zka.info/broken_thumbs/ (on a shocking pink background colour so you can see the difference between white and transparent).
Any bright ideas?
class Image {
public static function exec($path) {
// This function receives the path to an image.
// Logic here skipped: For each thumbnail, check whether Crop is set. If it is, call self::crop(). Otherwise, call self::resize().
return true;
}
public static function resize($file, $class, $width='-', $height='-', $fit=0) {
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'gif';
break;
case 2:
$tag = 'jpeg';
break;
case 3:
$tag = 'png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($width == 0) {
$fw = 0;
$fh = $h / $height;
} elseif ($height == 0) {
$fw = $w / $width;
$fh = 0;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw == 1) and ($fh == 1)) {
$file2 = dirname($file) . '/' . $class . basename($file);
copy($file, $file2);
return true;
} elseif (($fw >= 1) and ($fh >= 1)) {
if ($fw > $fh) {
$w = $width;
$h = floor($h / $fw);
} else {
$h = $height;
$w = floor($w / $fh);
}
} elseif ($fh == 0) {
if ($fw > 1) {
$fit = 0;
$w = $width;
$h = floor($h / $fw);
} else {
if ($fit) {
$height = $h;
}
}
} elseif ($fw == 0) {
if ($fh > 1) {
$fit = 0;
$w = floor($w / $fh);
$h = $height;
} else {
if ($fit) {
$width = $w;
}
}
} elseif (($fw < 1) and ($fh < 1)) {
//
} elseif ($fw > $fh) {
if ($fw >= 1) {
$w = $width;
$h = floor($h / $fw);
}
} elseif ($fh > $fw) {
if ($fh >= 1) {
$w = floor($w / $fh);
$h = $height;
}
}
if ($fit) {
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
$cw = $width;
$ch = $height;
} else {
$x = 0;
$y = 0;
$cw = $w;
$ch = $h;
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
$new = imagecreatetruecolor($cw, $ch);
return self::create($new, $src, $file2, $x, $y, $w, $h, $a);
}
public static function crop($file, $class, $width='-', $height='-', $fit=0) {
if (!$class) return trigger_error('ExecImage: Original image can not be overwritten.');
$small = 0;
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'gif';
break;
case 2:
$tag = 'jpeg';
break;
case 3:
$tag = 'png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($height == 0) {
//resize by width -- height will follow
$fh = 0;
$fw = $w / $width;
} elseif ($width == 0) {
//resize by height -- width will follow
$fw = 0;
$fh = $h / $height;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw <= 1) and ($fh <= 1)) {
$small = 1;
//don't resize
} else {
$fit = 1;
//chop by the smallest
if ($fh < $fw) {
//Crop By Height
$h = $height;
$w = floor($w / $fh);
//$w = $width;
//$h = floor($h /$fw);
//$w = $width;
} else {
//Crop By Width
$w = $width;
$h = floor($h /$fw);
//$h = $height;
//$w = floor($w / $fh);
}
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
if ($small) {
if ($fit) {
//image must be padded
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
} else {
//image goes as is -- shrinked
$x = 0;
$y = 0;
}
} else {
//image must be centered -- this should be a square from js
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
}
if ($small) {
if ($fit) {
//create with the full size
$new = imagecreatetruecolor($width, $height);
} else {
//nah, just with the original size
$new = imagecreatetruecolor($w, $h);
}
} else {
if ($fit) {
$new = imagecreatetruecolor($width, $height);
} else {
$new = imagecreatetruecolor($w, $h);
}
}
return self::create($new, $src, $file2, $x, $y, $w, $h, $a);
}
private static function create($new, $src, $file2, $x, $y, $w, $h, $a) {
switch ($a[2]) {
case 1: // GIF
case 3: // PNG
// http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
// http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/
imagealphablending($new, false);
imagesavealpha($new, true);
$back = imagecolorallocatealpha($new, 255, 255, 255, 127);
imagefilledrectangle($new, 0, 0, $w, $h, $back);
imagecolortransparent($new, $back);
break;
case 2: // JPEG
$back = imagecolorallocate($new, 255, 255, 255);
break;
}
imagefill($new, 0, 0, $back);
imagecopyresampled($new, $src, $x, $y, 0, 0, $w, $h, $a[0], $a[1]);
if (file_exists($file2)) unlink($file2);
switch ($a[2]) {
case 1:
imagegif($new, $file2);
break;
case 2:
imagejpeg($new, $file2, 100);
break;
case 3:
imagepng($new, $file2, 0);
break;
}
imagedestroy($src);
imagedestroy($new);
return true;
}
}
Revision/Edit to add:
Well, I've solved the main problem: padding added to PNG or GIF images is now transparent, instead of black. The smaller problem, that GIF transparency becomes black, is still with us, but I don't use GIF transparency much.
<?php
class Image {
public static function exec($path) {
$file = Nearest::file('.config.php', dirname($path) . '/', BD . '/images/');
if (!file_exists($file)) exit('The ".config.php" file doesnt exist');
require $file;
foreach ($config['operations'] as $k => $v) {
if (!isset($v['class'])) $v['class'] = '.' . $k . '.';
if (Core::val($v, 'crop')) {
self::crop($path, $v['class'], Core::val($v, 'width', '-'), Core::val($v, 'height', '-'), Core::val($v, 'fit', 0));
} else {
self::resize($path, $v['class'], Core::val($v, 'width', '-'), Core::val($v, 'height', '-'), Core::val($v, 'fit', 0));
}
}
return true;
}
public static function delete($path) {
$a = glob(dirname($path) . '/.*.' . basename($path));
foreach ($a as $v) unlink($v);
unlink($path);
}
public static function resize($file, $class, $width='-', $height='-', $fit=0) {
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'gif';
break;
case 2:
$tag = 'jpeg';
break;
case 3:
$tag = 'png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($width == 0) {
$fw = 0;
$fh = $h / $height;
} elseif ($height == 0) {
$fw = $w / $width;
$fh = 0;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw == 1) and ($fh == 1)) {
$file2 = dirname($file) . '/' . $class . basename($file);
copy($file, $file2);
return true;
} elseif (($fw >= 1) and ($fh >= 1)) {
if ($fw > $fh) {
$w = $width;
$h = floor($h / $fw);
} else {
$h = $height;
$w = floor($w / $fh);
}
} elseif ($fh == 0) {
if ($fw > 1) {
$fit = 0;
$w = $width;
$h = floor($h / $fw);
} else {
if ($fit) {
$height = $h;
}
}
} elseif ($fw == 0) {
if ($fh > 1) {
$fit = 0;
$w = floor($w / $fh);
$h = $height;
} else {
if ($fit) {
$width = $w;
}
}
} elseif (($fw < 1) and ($fh < 1)) {
//
} elseif ($fw > $fh) {
if ($fw >= 1) {
$w = $width;
$h = floor($h / $fw);
}
} elseif ($fh > $fw) {
if ($fh >= 1) {
$w = floor($w / $fh);
$h = $height;
}
}
if ($fit) {
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
$cw = $width;
$ch = $height;
} else {
$x = 0;
$y = 0;
$cw = $w;
$ch = $h;
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
$new = imagecreatetruecolor($cw, $ch);
return self::create($new, $src, $file2, $x, $y, $w, $h, $cw, $ch, $a);
}
public static function crop($file, $class, $width='-', $height='-', $fit=0) {
if (!$class) exit('ExecImage: Original image can not be overwrite.');
$small = 0;
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'Gif';
break;
case 2:
$tag = 'Jpeg';
break;
case 3:
$tag = 'Png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($height == 0) {
//resize by width -- height will follow
$fh = 0;
$fw = $w / $width;
} elseif ($width == 0) {
//resize by height -- width will follow
$fw = 0;
$fh = $h / $height;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw <= 1) and ($fh <= 1)) {
$small = 1;
//dont resize
} else {
$fit = 1;
//chop by the smallest
if ($fh < $fw) {
//Crop By Height
$h = $height;
$w = floor($w / $fh);
//$w = $width;
//$h = floor($h /$fw);
//$w = $width;
} else {
//Crop By Width
$w = $width;
$h = floor($h /$fw);
//$h = $height;
//$w = floor($w / $fh);
}
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'ImageCreateFrom' . $tag;
$src = $f1($file);
if ($small) {
if ($fit) {
//image must be padded
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
} else {
//image goes as is -- shrinked
$x = 0;
$y = 0;
}
} else {
//image must be centered -- this should be a square from js
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
}
if ($small) {
if ($fit) {
//create with the full size
$new = imagecreatetruecolor($width, $height);
return self::create($new, $src, $file2, $x, $y, $w, $h, $width, $height, $a);
} else {
//nah, just with the original size
$new = imagecreatetruecolor($w, $h);
return self::create($new, $src, $file2, $x, $y, $w, $h, $w, $h, $a);
}
} else {
if ($fit) {
//create with the full size
$new = imagecreatetruecolor($width, $height);
return self::create($new, $src, $file2, $x, $y, $w, $h, $width, $height, $a);
} else {
//nah, just with the original size
$new = imagecreatetruecolor($w, $h);
return self::create($new, $src, $file2, $x, $y, $w, $h, $w, $h, $a);
}
}
}
private static function create($new, $src, $file2, $x, $y, $w, $h, $cw, $ch, $a) {
switch ($a[2]) {
case 1: // GIF
case 3: // PNG
// http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
// http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/
imagealphablending($new, false);
imagesavealpha($new, true);
$back = imagecolorallocatealpha($new, 255, 255, 255, 127);
imagefilledrectangle($new, 0, 0, $cw, $ch, $back);
imagecolortransparent($new, $back);
break;
case 2: // JPEG
$back = imagecolorallocate($new, 255, 255, 255);
break;
}
imagefill($new, 0, 0, $back);
imagecopyresampled($new, $src, $x, $y, 0, 0, $w, $h, $a[0], $a[1]);
if (file_exists($file2)) unlink($file2);
switch ($a[2]) {
case 1:
imagegif($new, $file2);
break;
case 2:
imagejpeg($new, $file2, 100);
break;
case 3:
imagepng($new, $file2, 0);
break;
}
imagedestroy($src);
imagedestroy($new);
return true;
}
}
So, any hints on how to preserve GIF transparency?
You put a link with your answer ;)
You're using imageCopyResampled which acts weird with alpha. Use imageCopyResized instead.
http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/comment-page-1/#comment-94