* @package Mammut\Image * @version 1.0.3 */ class Image extends \Mammut\StrictObject { const _VERSION_ = '1.0.3'; const IPTC_ENVELOPE_CHARACTERSET = '1#090'; const IPTC_RECORD_VERSION = '2#000'; /* const Iptc.ObjectName 2#005 const Iptc.Category 2#015 const Iptc.Supplementals 2#020 const Iptc.Keywords 2#025 const Iptc.SpecialsInstructions 2#040 const Iptc.DateCreated 2#055 const Iptc.TimeCreated 2#060 const Iptc.DigitalCreationDate 2#062 const Iptc.DigitalCreationTime 2#063 const Iptc.ByLine 2#080 const Iptc.ByLineTitle 2#085 const Iptc.City 2#090 const Iptc.Sublocation 2#092 const Iptc.ProvinceState 2#095 const Iptc.CountryCode 2#100 const Iptc.CountryName 2#101 const Iptc.Headline 2#105 const Iptc.Credits 2#110 const Iptc.Source 2#115 const Iptc.Copyright 2#116 const Iptc.Contact 2#118 const Iptc.Caption 2#120 const Iptc.CaptionWriter 2#122 "2#000" => "Record Version", "2#003" => "Object Type Reference", "2#005" => "Object Name (Title)", "2#007" => "Edit Status", "2#008" => "Editorial Update", "2#010" => "Urgency", "2#012" => "Subject Reference", "2#015" => "Category", "2#020" => "Supplemental Category", "2#022" => "Fixture Identifier", "2#025" => "Keywords", "2#026" => "Content Location Code", "2#027" => "Content Location Name", "2#030" => "Release Date", "2#035" => "Release Time", "2#037" => "Expiration Date", "2#035" => "Expiration Time", "2#040" => "Special Instructions", "2#042" => "Action Advised", "2#045" => "Reference Service", "2#047" => "Reference Date", "2#050" => "Reference Number", "2#055" => "Date Created", "2#060" => "Time Created", "2#062" => "Digital Creation Date", "2#063" => "Digital Creation Time", "2#065" => "Originating Program", "2#070" => "Program Version", "2#075" => "Object Cycle", "2#080" => "By-Line (Author)", "2#085" => "By-Line Title (Author Position) [Not used in Photoshop 7]", "2#090" => "City", "2#092" => "Sub-Location", "2#095" => "Province/State", "2#100" => "Country/Primary Location Code", "2#101" => "Country/Primary Location Name", "2#103" => "Original Transmission Reference", "2#105" => "Headline", "2#110" => "Credit", "2#115" => "Source", "2#116" => "Copyright Notice", "2#118" => "Contact", "2#120" => "Caption/Abstract", "2#122" => "Caption Writer/Editor", "2#125" => "Rasterized Caption", "2#130" => "Image Type", "2#131" => "Image Orientation", "2#135" => "Language Identifier", "2#150" => "Audio Type", "2#151" => "Audio Sampling Rate", "2#152" => "Audio Sampling Resolution", "2#153" => "Audio Duration", "2#154" => "Audio Outcue", "2#200" => "ObjectData Preview File Format", "2#201" => "ObjectData Preview File Format Version", "2#202" => "ObjectData Preview Data" const IPTC_OBJECT_NAME= '005'; const IPTC_EDIT_STATUS= '007'; const IPTC_PRIORITY= '010'; const IPTC_CATEGORY= '015'; const IPTC_SUPPLEMENTAL_CATEGORY= '020'; const IPTC_FIXTURE_IDENTIFIER= '022'; const IPTC_KEYWORDS= '025'; const IPTC_RELEASE_DATE= '030'; const IPTC_RELEASE_TIME= '035'; const IPTC_SPECIAL_INSTRUCTIONS= '040'; const IPTC_REFERENCE_SERVICE= '045'; const IPTC_REFERENCE_DATE= '047'; const IPTC_REFERENCE_NUMBER= '050'; const IPTC_CREATED_DATE= '055'; const IPTC_CREATED_TIME= '060'; const IPTC_ORIGINATING_PROGRAM= '065'; const IPTC_PROGRAM_VERSION= '070'; const IPTC_OBJECT_CYCLE= '075'; const IPTC_BYLINE= '080'; const IPTC_BYLINE_TITLE= '085'; const IPTC_CITY= '090'; const IPTC_PROVINCE_STATE= '095'; const IPTC_COUNTRY_CODE= '100'; const IPTC_COUNTRY= '101'; const IPTC_ORIGINAL_TRANSMISSION_REFERENCE= '103'; const IPTC_HEADLINE= '105'; const IPTC_CREDIT= '110'; const IPTC_SOURCE= '115'; const IPTC_COPYRIGHT_STRING= '116'; const IPTC_CAPTION= '120'; const IPTC_LOCAL_CAPTION= '121';*/ protected $image; protected $color = false; protected $filename = false; private $alpha = 100; protected $meta = array(); public function __construct($file = false) { if(!extension_loaded('gd')) throw new ExtensionException('gd'); if($file) { if ($file instanceof iOutput) { $buffer = ''; while (!$file->isEOF()) $buffer .= $file->read(65535); $info = getimagesizefromstring($buffer); switch($info[2]) { case IMAGETYPE_PNG: if(imagetypes() & IMG_PNG != IMG_PNG) throw new \Exception('png support missing'); $this->image = imagecreatefromstring($buffer); imagealphablending($this->image, true); break; case IMAGETYPE_JPEG: if(imagetypes() & IMG_JPG != IMG_JPG) throw new \Exception('jpeg support missing'); $this->image = imagecreatefromstring($buffer); imagealphablending($this->image, true); break; default: throw new IOException('unsupported image type', IOException::IO_GENERAL, $file); break; } } elseif(file_exists($file)) { $info = getimagesize($file); switch($info[2]) { case IMAGETYPE_PNG: if(imagetypes() & IMG_PNG != IMG_PNG) throw new \Exception('png support missing'); $this->image = imagecreatefrompng($file); imagealphablending($this->image, true); break; case IMAGETYPE_JPEG: if(imagetypes() & IMG_JPG != IMG_JPG) throw new \Exception('jpeg support missing'); $this->image = imagecreatefromjpeg($file); /* $size = getimagesize($file,$info); var_dump($size,$info); if (isset($info["APP1"])) { $test = "EXIF\x00\x00"; if (strtoupper(substr($info["APP1"],0,strlen($test))) ==$test) { echo "EXIF0\n"; } $test = "EXIF\x00\xFF"; if (strtoupper(substr($info["APP1"],0,strlen($test))) ==$test) { echo "EXIF1\n"; } $test = "http://ns.adobe.com/xap/1.0/\x00"; if (strtoupper(substr($info["APP1"],0,strlen($test))) ==$test) { echo "XMP\n"; } var_dump(iptcparse ($info["APP1"])); } if (isset($info["APP13"])) { var_dump(iptcparse ($info["APP13"])); die(); } die();*/ imagealphablending($this->image, true); break; default: throw new IOException('unsupported image type', IOException::IO_GENERAL, $file); break; } } else throw new FileNotFoundException($file); } else $image = imagecreatetruecolor(1, 1); } public function clearMeta() { $this->meta = array(); } public function setMeta($key, $value) { $this->meta[$key] = $value; } public function getMeta($key) { return isset($this->meta[$key]) ? $this->meta[$key] : NULL; } public function destroy() { imagedestroy($this->image); $this->filename = NULL; $this->color = NULL; } public function setAlpha($num) { $this->alpha = $num; if($this->alpha > 100) $this->alpha = 100; elseif($this->alpha < 0) $this->alpha = 0; } public function getAlpha() { return $this->alpha; } public function resize($x, $y, $keepRatio = false) { $sizeX = imagesx($this->image); $sizeY = imagesy($this->image); $indexed = !imageistruecolor($this->image); if($keepRatio) { $ratio = $sizeX / $sizeY; $sizeX = $x; $sizeY = $x / $ratio; if($sizeY > $y) { $sizeY = $y; $sizeX = $y * $ratio; } unset($ratio); } else { $sizeX = $x; $sizeY = $y; } if(function_exists('imagecopyresampled') && !$indexed) { $tmp = imagecreatetruecolor($sizeX, $sizeY); imagecopyresampled($tmp, $this->image, 0, 0, 0, 0, $sizeX, $sizeY, imagesx($this->image), imagesy($this->image)); imagedestroy($this->image); $this->image = &$tmp; } elseif($indexed) { $tmp = imagecreate($sizeX, $sizeY); imagepalettecopy($tmp, $this->image); imagecopyresized($tmp, $this->image, 0, 0, 0, 0, $sizeX, $sizeY, imagesx($this->image), imagesy($this->image)); imagedestroy($this->image); $this->image = &$tmp; } else { $tmp = imagecreatetruecolor($sizeX, $sizeY); imagecopyresized($tmp, $this->image, 0, 0, 0, 0, $sizeX, $sizeY, imagesx($this->image), imagesy($this->image)); imagedestroy($this->image); $this->image = &$tmp; } } public function fill($color = false) { if($color == false) $color = $this->color; imagefilledrectangle($this->image, 0, 0, imagesx($this->image), imagesy($this->image), $color); } public function color($r, $g, $b, $alpha = 0) { if($alpha == 0) { $this->color = imagecolorallocate($this->image, $r, $g, $b); return $this->color; } else { $this->color = imagecolorallocatealpha($this->image, $r, $g, $b, $alpha); return $this->color; } } public function getImageResource() { return $this->image; } public function setImageResource($res) { $this->image = $res; } public function getWidth() { return imagesx($this->image); } public function getHeight() { return imagesy($this->image); } public function copy($x, $y, $w, $h) { $img = new Image($w, $h); $new = &$img->getImageResource(); imagecopy($new, $this->image, 0, 0, $x, $y, $w, $h); $img->setAlpha($this->getAlpha); return $img; } public function paste(Image $img, $x, $y) { imagecopymerge($this->image, $img->getImageResource(), $x, $y, 0, 0, $img->getWidth(), $img->getHeight(), $img->getAlpha()); } /** * Merge another image into this image * * @param Image $img * the other image resource * @param integer $x * 0 = top 1 = center 2 = bottom * @param integer $y * 0 = left 1 = center 2 = right * @param integer $addX * delta x * @param integer $addY * delta y * @param boolean $merge * true = imagecopymerge, false = imagecopy */ public function pasteRel(Image $img, $x, $y, $addX = 0, $addY = 0, $merge = false) { $px = 0; $py = 0; switch($x) { case 0: $px = 0; break; case 1: $pc = round($img->getWidth() / 2); $mc = round($this->getWidth() / 2); $px = $mc - $pc; break; case 2: $px = $this->getWidth() - $img->getWidth(); break; } switch($y) { case 0: $py = 0; break; case 1: $pc = round($img->getHeight() / 2); $mc = round($this->getHeight() / 2); $py = $mc - $pc; break; case 2: $py = $this->getHeight() - $img->getHeight(); break; } if($merge) imagecopymerge($this->image, $img->getImageResource(), $px + $addX, $py + $addY, 0, 0, $img->getWidth(), $img->getHeight(), $img->getAlpha()); else imagecopy($this->image, $img->getImageResource(), $px + $addX, $py + $addY, 0, 0, $img->getWidth(), $img->getHeight()); } public function __clone() { $img = new Image($this->getWidth(), $this->getHeight()); $img->past(0, 0, $this); return $img; } public function saveAsJPEG($filename, $quality = 80) { imagejpeg($this->image, $filename, $quality); } /** * Returns the image as a binary JPEG encoded string * @return string the image */ public function getAsJPEG($quality = 80) { $tmp = tempnam(sys_get_temp_dir(), 'img'); $this->saveAsJPEG($tmp, $quality); $data = file_get_contents($tmp); unlink($tmp); return $data; } public function saveAsPNG($filename, $quality = 100) { imagepng($this->image, $filename, $quality); } /** * Returns the image as a binary PNG encoded string * @return string the image */ public function getAsPNG($quality = 0) { $tmp = tempnam(sys_get_temp_dir(), 'img'); $this->saveAsPNG($tmp, $quality); $data = file_get_contents($tmp); unlink($tmp); return $data; } public function saveAsGIF($filename) { imagegif($this->image, $filename); } /** * Returns the image as a binary GIF encoded string * @return string the image */ public function getAsGIF() { $tmp = tempnam(sys_get_temp_dir(), 'img'); $this->saveAsPNG($tmp); $data = file_get_contents($tmp); unlink($tmp); return $data; } }