123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class ImageFile
- {
- var $id;
- var $filepath;
- var $filename;
- var $type;
- var $height;
- var $width;
- var $rotate=0;
- var $animated = null;
- function __construct($id=null, $filepath=null, $type=null, $width=null, $height=null)
- {
- $this->id = $id;
- $this->filepath = $filepath;
- $this->filename = basename($filepath);
- $info = @getimagesize($this->filepath);
- if (!(
- ($info[2] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) ||
- ($info[2] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) ||
- $info[2] == IMAGETYPE_BMP ||
- ($info[2] == IMAGETYPE_WBMP && function_exists('imagecreatefromwbmp')) ||
- ($info[2] == IMAGETYPE_XBM && function_exists('imagecreatefromxbm')) ||
- ($info[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')))) {
-
- throw new UnsupportedMediaException(_('Unsupported image format.'), $this->filepath);
- }
- $this->type = ($info) ? $info[2]:$type;
- $this->width = ($info) ? $info[0]:$width;
- $this->height = ($info) ? $info[1]:$height;
- if ($this->type == IMAGETYPE_JPEG && function_exists('exif_read_data')) {
-
- $exif = exif_read_data($this->filepath);
- if (is_array($exif) && isset($exif['Orientation'])) {
- switch ((int)$exif['Orientation']) {
- case 1:
- $this->rotate = 0;
- break;
- case 3:
- $this->rotate = 180;
- break;
- case 6:
- $this->rotate = -90;
- break;
- case 8:
- $this->rotate = 90;
- break;
- }
-
- }
- } elseif ($this->type === IMAGETYPE_GIF) {
- $this->animated = $this->isAnimatedGif();
- }
- Event::handle('FillImageFileMetadata', array($this));
- }
- public static function fromFileObject(File $file)
- {
- $imgPath = null;
- $media = common_get_mime_media($file->mimetype);
- if (Event::handle('CreateFileImageThumbnailSource', array($file, &$imgPath, $media))) {
- if (empty($file->filename)) {
- throw new UnsupportedMediaException(_('File without filename could not get a thumbnail source.'));
- }
- switch ($media) {
- case 'image':
- $imgPath = $file->getPath();
- break;
- default:
- throw new UnsupportedMediaException(_('Unsupported media format.'), $file->getPath());
- }
- }
- if (!file_exists($imgPath)) {
- throw new ServerException(sprintf('Image not available locally: %s', $imgPath));
- }
- try {
- $image = new ImageFile($file->id, $imgPath);
- } catch (UnsupportedMediaException $e) {
-
- if ($imgPath != $file->getPath()) {
- unlink($imgPath);
- }
- throw $e;
- }
- return $image;
- }
- public function getPath()
- {
- if (!file_exists($this->filepath)) {
- throw new ServerException('No file in ImageFile filepath');
- }
- return $this->filepath;
- }
- static function fromUpload($param='upload')
- {
- switch ($_FILES[$param]['error']) {
- case UPLOAD_ERR_OK:
- break;
- case UPLOAD_ERR_INI_SIZE:
- case UPLOAD_ERR_FORM_SIZE:
-
-
- throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'), ImageFile::maxFileSize()));
- case UPLOAD_ERR_PARTIAL:
- @unlink($_FILES[$param]['tmp_name']);
-
- throw new Exception(_('Partial upload.'));
- case UPLOAD_ERR_NO_FILE:
-
- default:
- common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']);
-
- throw new Exception(_('System error uploading file.'));
- }
- $info = @getimagesize($_FILES[$param]['tmp_name']);
- if (!$info) {
- @unlink($_FILES[$param]['tmp_name']);
-
- throw new UnsupportedMediaException(_('Not an image or corrupt file.'), '[deleted]');
- }
- return new ImageFile(null, $_FILES[$param]['tmp_name']);
- }
-
- function resize($size, $x = 0, $y = 0, $w = null, $h = null)
- {
- $targetType = $this->preferredType();
- $outname = Avatar::filename($this->id,
- image_type_to_extension($targetType),
- $size,
- common_timestamp());
- $outpath = Avatar::path($outname);
- $this->resizeTo($outpath, array('width'=>$size, 'height'=>$size,
- 'x'=>$x, 'y'=>$y,
- 'w'=>$w, 'h'=>$h));
- return $outname;
- }
-
- function copyTo($outpath)
- {
- return new ImageFile(null, $this->resizeTo($outpath));
- }
-
- function resizeTo($outpath, array $box=array())
- {
- $box['width'] = isset($box['width']) ? intval($box['width']) : $this->width;
- $box['height'] = isset($box['height']) ? intval($box['height']) : $this->height;
- $box['x'] = isset($box['x']) ? intval($box['x']) : 0;
- $box['y'] = isset($box['y']) ? intval($box['y']) : 0;
- $box['w'] = isset($box['w']) ? intval($box['w']) : $this->width;
- $box['h'] = isset($box['h']) ? intval($box['h']) : $this->height;
- if (!file_exists($this->filepath)) {
-
- throw new Exception(_('Lost our file.'));
- }
-
- if ($box['width'] === $this->width
- && $box['height'] === $this->height
- && $box['x'] === 0
- && $box['y'] === 0
- && $box['w'] === $this->width
- && $box['h'] === $this->height
- && $this->type == $this->preferredType()) {
- if ($this->rotate == 0) {
-
- @copy($this->filepath, $outpath);
- return $outpath;
- } elseif (abs($this->rotate) == 90) {
-
-
- $tmp = $box['width'];
- $box['width'] = $box['height'];
- $box['height'] = $tmp;
- $tmp = $box['x'];
- $box['x'] = $box['y'];
- $box['y'] = $tmp;
- $tmp = $box['w'];
- $box['w'] = $box['h'];
- $box['h'] = $tmp;
- }
- }
- if (Event::handle('StartResizeImageFile', array($this, $outpath, $box))) {
- $this->resizeToFile($outpath, $box);
- }
- if (!file_exists($outpath)) {
- throw new UseFileAsThumbnailException($this->id);
- }
- return $outpath;
- }
- protected function resizeToFile($outpath, array $box)
- {
- switch ($this->type) {
- case IMAGETYPE_GIF:
- $image_src = imagecreatefromgif($this->filepath);
- break;
- case IMAGETYPE_JPEG:
- $image_src = imagecreatefromjpeg($this->filepath);
- break;
- case IMAGETYPE_PNG:
- $image_src = imagecreatefrompng($this->filepath);
- break;
- case IMAGETYPE_BMP:
- $image_src = imagecreatefrombmp($this->filepath);
- break;
- case IMAGETYPE_WBMP:
- $image_src = imagecreatefromwbmp($this->filepath);
- break;
- case IMAGETYPE_XBM:
- $image_src = imagecreatefromxbm($this->filepath);
- break;
- default:
-
- throw new Exception(_('Unknown file type'));
- }
- if ($this->rotate != 0) {
- $image_src = imagerotate($image_src, $this->rotate, 0);
- }
- $image_dest = imagecreatetruecolor($box['width'], $box['height']);
- if ($this->type == IMAGETYPE_GIF || $this->type == IMAGETYPE_PNG || $this->type == IMAGETYPE_BMP) {
- $transparent_idx = imagecolortransparent($image_src);
- if ($transparent_idx >= 0) {
- $transparent_color = imagecolorsforindex($image_src, $transparent_idx);
- $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
- imagefill($image_dest, 0, 0, $transparent_idx);
- imagecolortransparent($image_dest, $transparent_idx);
- } elseif ($this->type == IMAGETYPE_PNG) {
- imagealphablending($image_dest, false);
- $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127);
- imagefill($image_dest, 0, 0, $transparent);
- imagesavealpha($image_dest, true);
- }
- }
- imagecopyresampled($image_dest, $image_src, 0, 0, $box['x'], $box['y'], $box['width'], $box['height'], $box['w'], $box['h']);
- switch ($this->preferredType()) {
- case IMAGETYPE_GIF:
- imagegif($image_dest, $outpath);
- break;
- case IMAGETYPE_JPEG:
- imagejpeg($image_dest, $outpath, common_config('image', 'jpegquality'));
- break;
- case IMAGETYPE_PNG:
- imagepng($image_dest, $outpath);
- break;
- default:
-
- throw new Exception(_('Unknown file type'));
- }
- imagedestroy($image_src);
- imagedestroy($image_dest);
- }
-
- function preferredType()
- {
- if($this->type == IMAGETYPE_BMP) {
-
-
- return IMAGETYPE_PNG;
- } else if($this->type == IMAGETYPE_WBMP) {
-
-
- return IMAGETYPE_PNG;
- } else if($this->type == IMAGETYPE_XBM) {
-
-
- return IMAGETYPE_PNG;
- }
- return $this->type;
- }
- function unlink()
- {
- @unlink($this->filepath);
- }
- static function maxFileSize()
- {
- $value = ImageFile::maxFileSizeInt();
- if ($value > 1024 * 1024) {
- $value = $value/(1024*1024);
-
- return sprintf(_m('%dMB','%dMB',$value),$value);
- } else if ($value > 1024) {
- $value = $value/1024;
-
- return sprintf(_m('%dkB','%dkB',$value),$value);
- } else {
-
- return sprintf(_m('%dB','%dB',$value),$value);
- }
- }
- static function maxFileSizeInt()
- {
- return min(ImageFile::strToInt(ini_get('post_max_size')),
- ImageFile::strToInt(ini_get('upload_max_filesize')),
- ImageFile::strToInt(ini_get('memory_limit')));
- }
- static function strToInt($str)
- {
- $unit = substr($str, -1);
- $num = substr($str, 0, -1);
- switch(strtoupper($unit)){
- case 'G':
- $num *= 1024;
- case 'M':
- $num *= 1024;
- case 'K':
- $num *= 1024;
- }
- return $num;
- }
- public function scaleToFit($maxWidth=null, $maxHeight=null, $crop=null)
- {
- return self::getScalingValues($this->width, $this->height,
- $maxWidth, $maxHeight, $crop, $this->rotate);
- }
-
- public static function getScalingValues($width, $height,
- $maxW=null, $maxH=null,
- $crop=null, $rotate=0)
- {
- $maxW = $maxW ?: common_config('thumbnail', 'width');
- $maxH = $maxH ?: common_config('thumbnail', 'height');
-
- if ($maxW < 1 || ($maxH !== null && $maxH < 1)) {
- throw new ServerException('Bad parameters for ImageFile::getScalingValues');
- } elseif ($maxH === null) {
-
- $maxH = $maxW;
- $crop = true;
- }
-
- if (abs($rotate) == 90) {
- $tmp = $width;
- $width = $height;
- $height = $tmp;
- }
-
-
-
- $cx = 0;
- $cy = 0;
- $cw = null;
- $ch = null;
-
- if ($crop) {
- $s_ar = $width / $height;
- $t_ar = $maxW / $maxH;
- $rw = $maxW;
- $rh = $maxH;
-
- if ($s_ar > $t_ar) {
- $cx = floor($width / 2 - $height * $t_ar / 2);
- $cw = ceil($height * $t_ar);
- } elseif ($s_ar < $t_ar) {
- $cy = floor($height / 2 - $width / $t_ar / 2);
- $ch = ceil($width / $t_ar);
- }
- } else {
- $rw = $maxW;
- $rh = ceil($height * $rw / $width);
-
- if ($rh > $maxH) {
- $rh = $maxH;
- $rw = ceil($width * $rh / $height);
- }
- }
- return array(intval($rw), intval($rh),
- intval($cx), intval($cy),
- is_null($cw) ? $width : intval($cw),
- is_null($ch) ? $height : intval($ch));
- }
-
- protected function isAnimatedGif()
- {
- if (!($fh = @fopen($this->filepath, 'rb'))) {
- return false;
- }
- $count = 0;
-
-
-
-
-
-
-
-
- while(!feof($fh) && $count < 2) {
- $chunk = fread($fh, 1024 * 100);
- $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);
-
-
- if (!feof($fh) && ftell($fh) >= 9) {
- fseek($fh, -9, SEEK_CUR);
- }
- }
- fclose($fh);
- return $count > 1;
- }
- }
- if(!function_exists('imagecreatefrombmp')){
-
- function imagecreatefrombmp($p_sFile)
- {
-
- $file = fopen($p_sFile,"rb");
- $read = fread($file,10);
- while(!feof($file)&&($read<>""))
- $read .= fread($file,1024);
- $temp = unpack("H*",$read);
- $hex = $temp[1];
- $header = substr($hex,0,108);
-
-
- if (substr($header,0,4)=="424d")
- {
-
- $header_parts = str_split($header,2);
-
- $width = hexdec($header_parts[19].$header_parts[18]);
-
- $height = hexdec($header_parts[23].$header_parts[22]);
-
- unset($header_parts);
- }
-
- $x = 0;
- $y = 1;
-
- $image = imagecreatetruecolor($width,$height);
-
- $body = substr($hex,108);
-
-
-
- $body_size = (strlen($body)/2);
- $header_size = ($width*$height);
-
- $usePadding = ($body_size>($header_size*3)+4);
-
-
- for ($i=0;$i<$body_size;$i+=3)
- {
-
- if ($x>=$width)
- {
-
-
- if ($usePadding)
- $i += $width%4;
-
- $x = 0;
-
- $y++;
-
- if ($y>$height)
- break;
- }
-
-
- $i_pos = $i*2;
- $r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
- $g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
- $b = hexdec($body[$i_pos].$body[$i_pos+1]);
-
- $color = imagecolorallocate($image,$r,$g,$b);
- imagesetpixel($image,$x,$height-$y,$color);
-
- $x++;
- }
-
- unset($body);
-
- return $image;
- }
- }
|