Merge pull request #11975 from owncloud/Combustible-combustible_dev

Check imagecreate*() return before using and use logger in \OC_Image
This commit is contained in:
Morris Jobke 2014-12-02 16:18:00 +01:00
commit a16d1a85ef
1 changed files with 267 additions and 254 deletions

View File

@ -1,25 +1,17 @@
<?php
/**
* ownCloud
*
* @author Thomas Tanghus
* @copyright 2011 Thomas Tanghus <thomas@tanghus.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
* ownCloud
*
* @author Thomas Tanghus
* @copyright 2011 Thomas Tanghus <thomas@tanghus.net>
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*
*/
/**
* Class for basic image manipulation
*/
@ -32,8 +24,14 @@ class OC_Image {
private $fileInfo;
/**
* @var \OCP\ILogger
*/
private $logger;
/**
* Get mime type for an image file.
*
* @param string|null $filePath The path to a local image file.
* @return string The mime type if the it could be determined, otherwise an empty string.
*/
@ -49,14 +47,19 @@ class OC_Image {
/**
* Constructor.
*
* @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
* an imagecreate* function.
* @return \OC_Image False on error
* @param \OCP\ILogger $logger
*/
public function __construct($imageRef = null) {
//OC_Log::write('core',__METHOD__.'(): start', OC_Log::DEBUG);
if(!extension_loaded('gd') || !function_exists('gd_info')) {
OC_Log::write('core', __METHOD__.'(): GD module not installed', OC_Log::ERROR);
public function __construct($imageRef = null, $logger = null) {
$this->logger = $logger;
if (is_null($logger)) {
$this->logger = \OC::$server->getLogger();
}
if (!extension_loaded('gd') || !function_exists('gd_info')) {
$this->logger->error(__METHOD__ . '(): GD module not installed', array('app' => 'core'));
return false;
}
@ -64,13 +67,14 @@ class OC_Image {
$this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
}
if(!is_null($imageRef)) {
if (!is_null($imageRef)) {
$this->load($imageRef);
}
}
/**
* Determine whether the object contains an image resource.
*
* @return bool
*/
public function valid() { // apparently you can't name a method 'empty'...
@ -79,6 +83,7 @@ class OC_Image {
/**
* Returns the MIME type of the image or an empty string if no image is loaded.
*
* @return string
*/
public function mimeType() {
@ -87,6 +92,7 @@ class OC_Image {
/**
* Returns the width of the image or -1 if no image is loaded.
*
* @return int
*/
public function width() {
@ -95,6 +101,7 @@ class OC_Image {
/**
* Returns the height of the image or -1 if no image is loaded.
*
* @return int
*/
public function height() {
@ -103,12 +110,13 @@ class OC_Image {
/**
* Returns the width when the image orientation is top-left.
*
* @return int
*/
public function widthTopLeft() {
$o = $this->getOrientation();
OC_Log::write('core', 'OC_Image->widthTopLeft() Orientation: '.$o, OC_Log::DEBUG);
switch($o) {
$this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core'));
switch ($o) {
case -1:
case 1:
case 2: // Not tested
@ -126,12 +134,13 @@ class OC_Image {
/**
* Returns the height when the image orientation is top-left.
*
* @return int
*/
public function heightTopLeft() {
$o = $this->getOrientation();
OC_Log::write('core', 'OC_Image->heightTopLeft() Orientation: '.$o, OC_Log::DEBUG);
switch($o) {
$this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core'));
switch ($o) {
case -1:
case 1:
case 2: // Not tested
@ -149,32 +158,34 @@ class OC_Image {
/**
* Outputs the image.
*
* @param string $mimeType
* @return bool
*/
public function show($mimeType=null) {
if($mimeType === null) {
public function show($mimeType = null) {
if ($mimeType === null) {
$mimeType = $this->mimeType();
}
header('Content-Type: '.$mimeType);
header('Content-Type: ' . $mimeType);
return $this->_output(null, $mimeType);
}
/**
* Saves the image.
*
* @param string $filePath
* @param string $mimeType
* @return bool
*/
public function save($filePath=null, $mimeType=null) {
if($mimeType === null) {
public function save($filePath = null, $mimeType = null) {
if ($mimeType === null) {
$mimeType = $this->mimeType();
}
if($filePath === null && $this->filePath === null) {
OC_Log::write('core', __METHOD__.'(): called with no path.', OC_Log::ERROR);
if ($filePath === null && $this->filePath === null) {
$this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core'));
return false;
} elseif($filePath === null && $this->filePath !== null) {
} elseif ($filePath === null && $this->filePath !== null) {
$filePath = $this->filePath;
}
return $this->_output($filePath, $mimeType);
@ -182,22 +193,21 @@ class OC_Image {
/**
* Outputs/saves the image.
*
* @param string $filePath
* @param string $mimeType
* @return bool
* @throws Exception
*/
private function _output($filePath=null, $mimeType=null) {
if($filePath) {
private function _output($filePath = null, $mimeType = null) {
if ($filePath) {
if (!file_exists(dirname($filePath)))
mkdir(dirname($filePath), 0777, true);
if(!is_writable(dirname($filePath))) {
OC_Log::write('core',
__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.',
OC_Log::ERROR);
if (!is_writable(dirname($filePath))) {
$this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core'));
return false;
} elseif(is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) {
OC_Log::write('core', __METHOD__.'(): File \''.$filePath.'\' is not writable.', OC_Log::ERROR);
} elseif (is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) {
$this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core'));
return false;
}
}
@ -206,8 +216,8 @@ class OC_Image {
}
$imageType = $this->imageType;
if($mimeType !== null) {
switch($mimeType) {
if ($mimeType !== null) {
switch ($mimeType) {
case 'image/gif':
$imageType = IMAGETYPE_GIF;
break;
@ -228,7 +238,7 @@ class OC_Image {
}
}
switch($imageType) {
switch ($imageType) {
case IMAGETYPE_GIF:
$retVal = imagegif($this->resource, $filePath);
break;
@ -289,11 +299,11 @@ class OC_Image {
break;
default:
$res = imagepng($this->resource);
OC_Log::write('core', 'OC_Image->data. Couldn\'t guess mimetype, defaulting to png', OC_Log::INFO);
$this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core'));
break;
}
if (!$res) {
OC_Log::write('core', 'OC_Image->data. Error getting image data.', OC_Log::ERROR);
$this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core'));
}
return ob_get_clean();
}
@ -308,30 +318,31 @@ class OC_Image {
/**
* (I'm open for suggestions on better method name ;)
* Get the orientation based on EXIF data.
*
* @return int The orientation or -1 if no EXIF data is available.
*/
public function getOrientation() {
if ($this->imageType !== IMAGETYPE_JPEG) {
OC_Log::write('core', 'OC_Image->fixOrientation() Image is not a JPEG.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core'));
return -1;
}
if(!is_callable('exif_read_data')) {
OC_Log::write('core', 'OC_Image->fixOrientation() Exif module not enabled.', OC_Log::DEBUG);
if (!is_callable('exif_read_data')) {
$this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
return -1;
}
if(!$this->valid()) {
OC_Log::write('core', 'OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG);
if (!$this->valid()) {
$this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
return -1;
}
if(is_null($this->filePath) || !is_readable($this->filePath)) {
OC_Log::write('core', 'OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
if (is_null($this->filePath) || !is_readable($this->filePath)) {
$this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core'));
return -1;
}
$exif = @exif_read_data($this->filePath, 'IFD0');
if(!$exif) {
if (!$exif) {
return -1;
}
if(!isset($exif['Orientation'])) {
if (!isset($exif['Orientation'])) {
return -1;
}
return $exif['Orientation'];
@ -340,13 +351,14 @@ class OC_Image {
/**
* (I'm open for suggestions on better method name ;)
* Fixes orientation based on EXIF data.
*
* @return bool.
*/
public function fixOrientation() {
$o = $this->getOrientation();
OC_Log::write('core', 'OC_Image->fixOrientation() Orientation: '.$o, OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core'));
$rotate = 0;
switch($o) {
switch ($o) {
case -1:
return false; //Nothing to fix
case 1:
@ -375,24 +387,24 @@ class OC_Image {
$rotate = 90;
break;
}
if($rotate) {
if ($rotate) {
$res = imagerotate($this->resource, $rotate, 0);
if($res) {
if(imagealphablending($res, true)) {
if(imagesavealpha($res, true)) {
if ($res) {
if (imagealphablending($res, true)) {
if (imagesavealpha($res, true)) {
imagedestroy($this->resource);
$this->resource = $res;
return true;
} else {
OC_Log::write('core', 'OC_Image->fixOrientation() Error during alphasaving.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core'));
return false;
}
} else {
OC_Log::write('core', 'OC_Image->fixOrientation() Error during alphablending.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core'));
return false;
}
} else {
OC_Log::write('core', 'OC_Image->fixOrientation() Error during oriention fixing.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core'));
return false;
}
}
@ -401,52 +413,54 @@ class OC_Image {
/**
* Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
*
* @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle ).
* @return resource|false An image resource or false on error
*/
public function load($imageRef) {
if(is_resource($imageRef)) {
if(get_resource_type($imageRef) == 'gd') {
if (is_resource($imageRef)) {
if (get_resource_type($imageRef) == 'gd') {
$this->resource = $imageRef;
return $this->resource;
} elseif(in_array(get_resource_type($imageRef), array('file', 'stream'))) {
} elseif (in_array(get_resource_type($imageRef), array('file', 'stream'))) {
return $this->loadFromFileHandle($imageRef);
}
} elseif($this->loadFromBase64($imageRef) !== false) {
} elseif ($this->loadFromBase64($imageRef) !== false) {
return $this->resource;
} elseif($this->loadFromFile($imageRef) !== false) {
} elseif ($this->loadFromFile($imageRef) !== false) {
return $this->resource;
} elseif($this->loadFromData($imageRef) !== false) {
} elseif ($this->loadFromData($imageRef) !== false) {
return $this->resource;
} else {
OC_Log::write('core', __METHOD__.'(): couldn\'t load anything. Giving up!', OC_Log::DEBUG);
return false;
}
$this->logger->debug(__METHOD__ . '(): could not load anything. Giving up!', array('app' => 'core'));
return false;
}
/**
* Loads an image from an open file handle.
* It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
*
* @param resource $handle
* @return resource|false An image resource or false on error
*/
public function loadFromFileHandle($handle) {
OC_Log::write('core', __METHOD__.'(): Trying', OC_Log::DEBUG);
$contents = stream_get_contents($handle);
if($this->loadFromData($contents)) {
if ($this->loadFromData($contents)) {
return $this->resource;
}
return false;
}
/**
* Loads an image from a local file.
*
* @param bool|string $imagePath The path to a local file.
* @return bool|resource An image resource or false on error
*/
public function loadFromFile($imagePath=false) {
public function loadFromFile($imagePath = false) {
// exif_imagetype throws "read error!" if file is less than 12 byte
if(!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
OC_Log::write('core', 'OC_Image->loadFromFile, couldn\'t load: ' . (string) urlencode($imagePath), OC_Log::DEBUG);
if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
$this->logger->debug('OC_Image->loadFromFile, could not load: ' . (string)urlencode($imagePath), array('app' => 'core'));
return false;
}
$iType = exif_imagetype($imagePath);
@ -458,18 +472,14 @@ class OC_Image {
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, GIF images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_JPEG:
if (imagetypes() & IMG_JPG) {
$this->resource = imagecreatefromjpeg($imagePath);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, JPG images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_PNG:
@ -479,27 +489,21 @@ class OC_Image {
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, PNG images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_XBM:
if (imagetypes() & IMG_XPM) {
$this->resource = imagecreatefromxbm($imagePath);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_WBMP:
if (imagetypes() & IMG_WBMP) {
$this->resource = imagecreatefromwbmp($imagePath);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, WBMP images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_BMP:
@ -534,10 +538,10 @@ class OC_Image {
// this is mostly file created from encrypted file
$this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
$iType = IMAGETYPE_PNG;
OC_Log::write('core', 'OC_Image->loadFromFile, Default', OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
break;
}
if($this->valid()) {
if ($this->valid()) {
$this->imageType = $iType;
$this->mimeType = image_type_to_mime_type($iType);
$this->filePath = $imagePath;
@ -547,24 +551,25 @@ class OC_Image {
/**
* Loads an image from a string of data.
*
* @param string $str A string of image data as read from a file.
* @return bool|resource An image resource or false on error
*/
public function loadFromData($str) {
if(is_resource($str)) {
if (is_resource($str)) {
return false;
}
$this->resource = @imagecreatefromstring($str);
if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($str);
}
if(is_resource($this->resource)) {
if (is_resource($this->resource)) {
imagealphablending($this->resource, false);
imagesavealpha($this->resource, true);
}
if(!$this->resource) {
OC_Log::write('core', 'OC_Image->loadFromData, couldn\'t load', OC_Log::DEBUG);
if (!$this->resource) {
$this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core'));
return false;
}
return $this->resource;
@ -572,21 +577,22 @@ class OC_Image {
/**
* Loads an image from a base64 encoded string.
*
* @param string $str A string base64 encoded string of image data.
* @return bool|resource An image resource or false on error
*/
public function loadFromBase64($str) {
if(!is_string($str)) {
if (!is_string($str)) {
return false;
}
$data = base64_decode($str);
if($data) { // try to load from string data
if ($data) { // try to load from string data
$this->resource = @imagecreatefromstring($data);
if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($data);
}
if(!$this->resource) {
OC_Log::write('core', 'OC_Image->loadFromBase64, couldn\'t load', OC_Log::DEBUG);
if (!$this->resource) {
$this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core'));
return false;
}
return $this->resource;
@ -597,6 +603,7 @@ class OC_Image {
/**
* Create a new image from file or URL
*
* @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm
* @version 1.00
* @param string $fileName <p>
@ -606,7 +613,7 @@ class OC_Image {
*/
private function imagecreatefrombmp($fileName) {
if (!($fh = fopen($fileName, 'rb'))) {
trigger_error('imagecreatefrombmp: Can not open ' . $fileName, E_USER_WARNING);
$this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core'));
return false;
}
// read file header
@ -614,7 +621,7 @@ class OC_Image {
// check for bitmap
if ($meta['type'] != 19778) {
fclose($fh);
trigger_error('imagecreatefrombmp: ' . $fileName . ' is not a bitmap!', E_USER_WARNING);
$this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
return false;
}
// read image header
@ -626,7 +633,7 @@ class OC_Image {
// set bytes and padding
$meta['bytes'] = $meta['bits'] / 8;
$this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call
$meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4)- floor($meta['width'] * $meta['bytes'] / 4)));
$meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4)));
if ($meta['decal'] == 4) {
$meta['decal'] = 0;
}
@ -638,7 +645,7 @@ class OC_Image {
$meta['imagesize'] = @filesize($fileName) - $meta['offset'];
if ($meta['imagesize'] < 1) {
fclose($fh);
trigger_error('imagecreatefrombmp: Can not obtain filesize of ' . $fileName . '!', E_USER_WARNING);
$this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
return false;
}
}
@ -658,6 +665,14 @@ class OC_Image {
}
// create gd image
$im = imagecreatetruecolor($meta['width'], $meta['height']);
if ($im == false) {
fclose($fh);
$this->logger->warning(
'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'],
array('app' => 'core'));
return false;
}
$data = fread($fh, $meta['imagesize']);
$p = 0;
$vide = chr(0);
@ -671,7 +686,7 @@ class OC_Image {
case 32:
case 24:
if (!($part = substr($data, $p, 3))) {
trigger_error($error, E_USER_WARNING);
$this->logger->warning($error, array('app' => 'core'));
return $im;
}
$color = unpack('V', $part . $vide);
@ -679,7 +694,7 @@ class OC_Image {
case 16:
if (!($part = substr($data, $p, 2))) {
fclose($fh);
trigger_error($error, E_USER_WARNING);
$this->logger->warning($error, array('app' => 'core'));
return $im;
}
$color = unpack('v', $part);
@ -687,12 +702,12 @@ class OC_Image {
break;
case 8:
$color = unpack('n', $vide . substr($data, $p, 1));
$color[1] = $palette[ $color[1] + 1 ];
$color[1] = $palette[$color[1] + 1];
break;
case 4:
$color = unpack('n', $vide . substr($data, floor($p), 1));
$color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F;
$color[1] = $palette[ $color[1] + 1 ];
$color[1] = $palette[$color[1] + 1];
break;
case 1:
$color = unpack('n', $vide . substr($data, floor($p), 1));
@ -722,13 +737,11 @@ class OC_Image {
$color[1] = ($color[1] & 0x1);
break;
}
$color[1] = $palette[ $color[1] + 1 ];
$color[1] = $palette[$color[1] + 1];
break;
default:
fclose($fh);
trigger_error('imagecreatefrombmp: '
. $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!',
E_USER_WARNING);
$this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core'));
return false;
}
imagesetpixel($im, $x, $y, $color[1]);
@ -744,23 +757,24 @@ class OC_Image {
/**
* Resizes the image preserving ratio.
*
* @param integer $maxSize The maximum size of either the width or height.
* @return bool
*/
public function resize($maxSize) {
if(!$this->valid()) {
OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR);
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
return false;
}
$widthOrig=imageSX($this->resource);
$heightOrig=imageSY($this->resource);
$ratioOrig = $widthOrig/$heightOrig;
$widthOrig = imageSX($this->resource);
$heightOrig = imageSY($this->resource);
$ratioOrig = $widthOrig / $heightOrig;
if ($ratioOrig > 1) {
$newHeight = round($maxSize/$ratioOrig);
$newHeight = round($maxSize / $ratioOrig);
$newWidth = $maxSize;
} else {
$newWidth = round($maxSize*$ratioOrig);
$newWidth = round($maxSize * $ratioOrig);
$newHeight = $maxSize;
}
@ -775,21 +789,21 @@ class OC_Image {
*/
public function preciseResize($width, $height) {
if (!$this->valid()) {
OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR);
$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
return false;
}
$widthOrig=imageSX($this->resource);
$heightOrig=imageSY($this->resource);
$widthOrig = imageSX($this->resource);
$heightOrig = imageSY($this->resource);
$process = imagecreatetruecolor($width, $height);
if ($process == false) {
OC_Log::write('core', __METHOD__.'(): Error creating true color image', OC_Log::ERROR);
$this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
imagedestroy($process);
return false;
}
// preserve transparency
if($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
imagealphablending($process, false);
imagesavealpha($process, true);
@ -797,7 +811,7 @@ class OC_Image {
imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
if ($process == false) {
OC_Log::write('core', __METHOD__.'(): Error resampling process image '.$width.'x'.$height, OC_Log::ERROR);
$this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core'));
imagedestroy($process);
return false;
}
@ -808,45 +822,46 @@ class OC_Image {
/**
* Crops the image to the middle square. If the image is already square it just returns.
*
* @param int $size maximum size for the result (optional)
* @return bool for success or failure
*/
public function centerCrop($size=0) {
if(!$this->valid()) {
OC_Log::write('core', 'OC_Image->centerCrop, No image loaded', OC_Log::ERROR);
public function centerCrop($size = 0) {
if (!$this->valid()) {
$this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core'));
return false;
}
$widthOrig=imageSX($this->resource);
$heightOrig=imageSY($this->resource);
if($widthOrig === $heightOrig and $size==0) {
$widthOrig = imageSX($this->resource);
$heightOrig = imageSY($this->resource);
if ($widthOrig === $heightOrig and $size == 0) {
return true;
}
$ratioOrig = $widthOrig/$heightOrig;
$ratioOrig = $widthOrig / $heightOrig;
$width = $height = min($widthOrig, $heightOrig);
if ($ratioOrig > 1) {
$x = ($widthOrig/2) - ($width/2);
$x = ($widthOrig / 2) - ($width / 2);
$y = 0;
} else {
$y = ($heightOrig/2) - ($height/2);
$y = ($heightOrig / 2) - ($height / 2);
$x = 0;
}
if($size>0) {
$targetWidth=$size;
$targetHeight=$size;
}else{
$targetWidth=$width;
$targetHeight=$height;
if ($size > 0) {
$targetWidth = $size;
$targetHeight = $size;
} else {
$targetWidth = $width;
$targetHeight = $height;
}
$process = imagecreatetruecolor($targetWidth, $targetHeight);
if ($process == false) {
OC_Log::write('core', 'OC_Image->centerCrop. Error creating true color image', OC_Log::ERROR);
$this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core'));
imagedestroy($process);
return false;
}
// preserve transparency
if($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
imagealphablending($process, false);
imagesavealpha($process, true);
@ -854,9 +869,7 @@ class OC_Image {
imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
if ($process == false) {
OC_Log::write('core',
'OC_Image->centerCrop. Error resampling process image '.$width.'x'.$height,
OC_Log::ERROR);
$this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core'));
imagedestroy($process);
return false;
}
@ -867,6 +880,7 @@ class OC_Image {
/**
* Crops the image from point $x$y with dimension $wx$h.
*
* @param int $x Horizontal position
* @param int $y Vertical position
* @param int $w Width
@ -874,19 +888,19 @@ class OC_Image {
* @return bool for success or failure
*/
public function crop($x, $y, $w, $h) {
if(!$this->valid()) {
OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR);
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
return false;
}
$process = imagecreatetruecolor($w, $h);
if ($process == false) {
OC_Log::write('core', __METHOD__.'(): Error creating true color image', OC_Log::ERROR);
$this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
imagedestroy($process);
return false;
}
// preserve transparency
if($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
imagealphablending($process, false);
imagesavealpha($process, true);
@ -894,7 +908,7 @@ class OC_Image {
imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
if ($process == false) {
OC_Log::write('core', __METHOD__.'(): Error resampling process image '.$w.'x'.$h, OC_Log::ERROR);
$this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core'));
imagedestroy($process);
return false;
}
@ -904,41 +918,44 @@ class OC_Image {
}
/**
* Resizes the image to fit within a boundry while preserving ratio.
* Resizes the image to fit within a boundary while preserving ratio.
*
* @param integer $maxWidth
* @param integer $maxHeight
* @return bool
*/
public function fitIn($maxWidth, $maxHeight) {
if(!$this->valid()) {
OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR);
if (!$this->valid()) {
$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
return false;
}
$widthOrig=imageSX($this->resource);
$heightOrig=imageSY($this->resource);
$ratio = $widthOrig/$heightOrig;
$widthOrig = imageSX($this->resource);
$heightOrig = imageSY($this->resource);
$ratio = $widthOrig / $heightOrig;
$newWidth = min($maxWidth, $ratio*$maxHeight);
$newHeight = min($maxHeight, $maxWidth/$ratio);
$newWidth = min($maxWidth, $ratio * $maxHeight);
$newHeight = min($maxHeight, $maxWidth / $ratio);
$this->preciseResize(round($newWidth), round($newHeight));
return true;
}
public function destroy() {
if($this->valid()) {
if ($this->valid()) {
imagedestroy($this->resource);
}
$this->resource=null;
$this->resource = null;
}
public function __destruct() {
$this->destroy();
}
}
if ( ! function_exists( 'imagebmp') ) {
if (!function_exists('imagebmp')) {
/**
* Output a BMP image to either the browser or a file
*
* @link http://www.ugia.cn/wp-data/imagebmp.php
* @author legend <legendsky@hotmail.com>
* @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
@ -949,11 +966,10 @@ if ( ! function_exists( 'imagebmp') ) {
* @param int $compression [optional]
* @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
function imagebmp($im, $fileName='', $bit=24, $compression=0) {
function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) {
if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) {
$bit = 24;
}
else if ($bit == 32) {
} else if ($bit == 32) {
$bit = 24;
}
$bits = pow(2, $bit);
@ -975,7 +991,7 @@ if ( ! function_exists( 'imagebmp') ) {
if ($padding % 4 != 0) {
$extra = str_repeat("\0", $padding);
}
for ($j = $height - 1; $j >= 0; $j --) {
for ($j = $height - 1; $j >= 0; $j--) {
$i = 0;
while ($i < $width) {
$bin = 0;
@ -989,8 +1005,7 @@ if ( ! function_exists( 'imagebmp') ) {
}
$bmpData .= $extra;
}
}
// RLE8
} // RLE8
else if ($compression == 1 && $bit == 8) {
for ($j = $height - 1; $j >= 0; $j--) {
$lastIndex = "\0";
@ -1003,8 +1018,7 @@ if ( ! function_exists( 'imagebmp') ) {
}
$lastIndex = $index;
$sameNum = 1;
}
else {
} else {
$sameNum++;
}
}
@ -1014,8 +1028,7 @@ if ( ! function_exists( 'imagebmp') ) {
}
$sizeQuad = strlen($rgbQuad);
$sizeData = strlen($bmpData);
}
else {
} else {
$extra = '';
$padding = 4 - ($width * ($bit / 8)) % 4;
if ($padding % 4 != 0) {
@ -1032,8 +1045,7 @@ if ( ! function_exists( 'imagebmp') ) {
$bin |= ($colors['green'] >> 3) << 5;
$bin |= $colors['blue'] >> 3;
$bmpData .= pack("v", $bin);
}
else {
} else {
$bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
}
}
@ -1051,20 +1063,21 @@ if ( ! function_exists( 'imagebmp') ) {
fclose($fp);
return true;
}
echo $fileHeader . $infoHeader. $rgbQuad . $bmpData;
echo $fileHeader . $infoHeader . $rgbQuad . $bmpData;
return true;
}
}
if ( ! function_exists( 'exif_imagetype' ) ) {
if (!function_exists('exif_imagetype')) {
/**
* Workaround if exif_imagetype does not exist
*
* @link http://www.php.net/manual/en/function.exif-imagetype.php#80383
* @param string $fileName
* @return string|boolean
*/
function exif_imagetype ( $fileName ) {
if ( ( $info = getimagesize( $fileName ) ) !== false ) {
function exif_imagetype($fileName) {
if (($info = getimagesize($fileName)) !== false) {
return $info[2];
}
return false;