<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// WebSite: http://www.rvglobalsoft.com
// Unauthorized copying is strictly forbidden and may result in severe legal action.
// Copyright (c) 2006 RV Global Soft Co.,Ltd. All rights reserved.
//
// =====YOU MUST KEEP THIS COPYRIGHTS NOTICE INTACT AND CAN NOT BE REMOVE =======
// Copyright (c) 2006 RV Global Soft Co.,Ltd. All rights reserved.
// This Agreement is a legal contract, which specifies the terms of the license
// and warranty limitation between you and RV Global Soft Co.,Ltd. and RV Site Builder.
// You should carefully read the following terms and conditions before
// installing or using this software. Unless you have a different license
// agreement obtained from RV Global Soft Co.,Ltd., installation or use of this software
// indicates your acceptance of the license and warranty limitation terms
// contained in this Agreement. If you do not agree to the terms of this
// Agreement, promptly delete and destroy all copies of the Software.
//
// ===== Grant of License =======
// The Software may only be installed and used on a single host machine.
//
// ===== Disclaimer of Warranty =======
// THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND
// WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER
// WARRANTIES WHETHER EXPRESSED OR IMPLIED. BECAUSE OF THE VARIOUS HARDWARE
// AND SOFTWARE ENVIRONMENTS INTO WHICH RV SITE BUILDER MAY BE USED, NO WARRANTY OF
// FITNESS FOR A PARTICULAR PURPOSE IS OFFERED. THE USER MUST ASSUME THE
// ENTIRE RISK OF USING THIS PROGRAM. ANY LIABILITY OF RV GLOBAL SOFT CO.,LTD. WILL BE
// LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE.
// IN NO CASE SHALL RV GLOBAL SOFT CO.,LTD. BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR
// CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS
// OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE
// BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT,
// NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF
// RV GLOBAL SOFT CO.,LTD. IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL
// RV GLOBAL SOFT CO.,LTD.'S LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID
// BY LICENSEE TO RV GLOBAL SOFT CO.,LTD.
// +---------------------------------------------------------------------------+
// $Id$
// +---------------------------------------------------------------------------+
/**
* RvSiteBuilder Image2 libary
*
* @package RvsLibs
* @author Pairote Manunphol <pairote@rvglobalsoft.com>
* @version $Revision$
* @since PHP 5.x
*/
if (class_exists('RvsLibs_Image2') === false) {
class RvsLibs_Image2
{
/**
* แนวตั้ง
* getPositionCrop 3x4 - Get position crop image vertical 3:4
* @param Integer $width - Width image
* @param Integer $height - Heigth image
* @param Integer $maxPixel - Max pixel for control position crop image
* @return Array $aPositionCrop3x4 - $aPositionCrop3x4[0] value position X image
* - $aPositionCrop3x4[1] value position Y image
* - $aPositionCrop3x4[2] value position W image
* - $aPositionCrop3x4[3] value position H image
* - $aPositionCrop3x4['makeBackgroundImage'] If height >= maxPixel return FALSE
* else return TRUE
*
*/
public static function getPositionCrop3x4($width, $height, $maxPixel)
{
$aPositionCrop3x4 = array(0, 0,$width, $height, 'makeBackgroundImage' => true);
if ($height >= $maxPixel) {
$aPositionCrop3x4[2] = ((floor($height/4)*3) < $width)
? (floor($height/4)*3)
: $width;//Position W
$aPositionCrop3x4['makeBackgroundImage'] = false;
}
return $aPositionCrop3x4;
}
/**
* แนวนอน
* getPositionCrop 4x3 - Get position crop image vertical 4:3
* @param Integer $width - Width image
* @param Integer $height - Heigth image
* @param Integer $maxPixel - Max pixel for control position crop image
* @return Array $aPositionCrop4x3 - $aPositionCrop4x3[0] value position X image
*- $aPositionCrop4x3[1] value position Y image
*- $aPositionCrop4x3[2] value position W image
*- $aPositionCrop4x3[3] value position H image
*- $aPositionCrop4x3['makeBackgroundImage'] If width >= maxPixel return FALSE
* else return TRUE
*
*/
public static function getPositionCrop4x3($width, $height, $maxPixel)
{
$aPositionCrop4x3 = array(0, 0, $width, $height, 'makeBackgroundImage' => true);
if ($width >= $maxPixel) {
$aPositionCrop4x3[3] = ((floor($width/4)*3) < $height)
? (floor($width/4)*3)
: $height;//Position H
$aPositionCrop4x3['makeBackgroundImage'] = false;
}
return $aPositionCrop4x3;
}
/**
* return Boolean - If HORIZONTAL return TRUE
* - else VERTICAL return FALSE
*/
public static function isRange($width, $height)
{
return ($width >= $height) ? true : false;
}
/**
* Get Position Auto Crop Ratio
* @param Integer $width
* @param Integer $height
* @param Integer $maxPixel
* @return Array If image horizontal RETURN $aPositionCrop4x3
* else image vertical RETURN $aPositionCrop3x4
*/
public static function getPositionAutoCropRatio($width, $height, $maxPixel)
{
if (RvsLibs_Image2::isRange($width, $height) == true) {//แนวนอน
return RvsLibs_Image2::getPositionCrop4x3($width, $height, $maxPixel);
} else {//แนวตั้ง
return RvsLibs_Image2::getPositionCrop3x4($width, $height, $maxPixel);
}
}
/**
* Get Positon Zone 3x3 Auto Crop
* @param Integer$width
* @param Integer $height
* @param Integert $maxPixel
* @return Array $aPositionAutoCrop - $aPositionAutoCrop[0] value position X image
* - $aPositionAutoCrop[1] value position Y image
* - $aPositionAutoCrop[2] value position W image
* - $aPositionAutoCrop[3] value position H image
* - $aPositionAutoCrop['makeBackgroundImage'] If width >= maxPixel OR height >= maxPixel return FALSE
* else return TRUE
*/
public static function getPositionZone3x3AutoCrop($width, $height, $maxPixel)
{
$aPositionAutoCrop = array(0, 0,$width, $height, 'makeBackgroundImage' => true);
if (RvsLibs_Image2::isRange($width, $height) == true) {//แนวนอน
if ($width >= $maxPixel) {
$aPositionAutoCrop[0] = floor(($width/3)/4);//Position X
$aPositionAutoCrop[1] = floor(($height/3)/4);//Position Y
$aPositionAutoCrop[2] = $width - ($aPositionAutoCrop[0] + floor(($width/3)/8));//Position W
$aPositionAutoCrop[3] = $height - ($aPositionAutoCrop[1] + floor((($height/3)/4)*3));//Position H
$aPositionAutoCrop['makeBackgroundImage'] = false;
}
} else {//แนวตั้ง
if ($height >= $maxPixel) {
$aPositionAutoCrop['makeBackgroundImage'] = false;
}
}
return $aPositionAutoCrop;
}
/**
* Get Position Auto Thumb
* @param Integer $width
* @param Integer $height
* @param Integer $maxPixel
* @return Array $aPositionAutoThumb - $aPositionAutoThumb[0] value position X image
* - $aPositionAutoThumb[1] value position Y image
* - $aPositionAutoThumb['makeBackgroundImage'] If makeBackgroundImage RETURN TRUE
* else FALSE
*/
public static function getPositionAutoThumb($width, $height, $maxPixel, $makeBackgroundImage=true)
{
if ($maxPixel == 0) {
return array($width, $height, 'makeBackgroundImage' => false);
}
$aPositionAutoThumb = array($maxPixel, $maxPixel, 'makeBackgroundImage' => $makeBackgroundImage);
if (RvsLibs_Image2::isRange($width, $height) == true) {//แนวนอน
if ($width >= $maxPixel) {
$res = $width/$maxPixel;
$aPositionAutoThumb[0] = $maxPixel;
$aPositionAutoThumb[1] = floor($height/$res);
$aPositionAutoThumb['makeBackgroundImage'] = false;
}
} else {//แนวตั้ง
if ($height >= $maxPixel) {
$res = $height/$maxPixel;
$aPositionAutoThumb[0] = floor($width/$res);
$aPositionAutoThumb[1] = $maxPixel;
$aPositionAutoThumb['makeBackgroundImage'] = false;
}
}
return $aPositionAutoThumb;
}
/**
* makeResizeBySizeRandom
*
* @param $fileImgSource
* @param $fileImgDest
* @param $maxSize (type KB)
* @param $createThumbnailBaseOn - Driver {GD, IM}. set Default = GD
* @return Boolean true
*/
public static function makeResizeBySizeRandom($fileImgSource, $fileImgDest, $maxSize, $createThumbnailBaseOn='gd')
{
SGL::logMessage('********file dest : ' . $fileImgDest, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
$bool = true;
$randomQuality = 100;
$countEndLoop = 0;
//COMPARE TYPE BYTE
if (is_file($fileImgSource) === true && (filesize($fileImgSource) >= ($maxSize*1024))) {
SGL::logMessage('case if RVsupportResize :: filesize ' . filesize($fileImgSource) . ' maxsize = ' . $maxSize*1024 . ' Loop = ' . $countEndLoop, PEAR_LOG_DEBUG);
while ($bool) {
$countEndLoop++;
$res = $objImage->load($fileImgSource);
if ($res === false) {
continue;
}
$aImgSize = $objImage->getImageSize();
$sizeImgSourceTypeByte = filesize($fileImgSource);
//CONVERT KB TO BYTE
$maxSizeTypeByte = $maxSize*1024;
if ($sizeImgSourceTypeByte > $maxSizeTypeByte) {
$objImage->resize($aImgSize[0], $aImgSize[1]);
$objImage->save($fileImgDest, $objImage->getImageType(), $randomQuality);
$objImage->free();
if (filesize($fileImgDest) > $maxSizeTypeByte) {
$bool = true;
$randomQuality -= 5;
} else {
$bool = false;
}
} else {
$bool = false;
}
//ในกรณีที่ bool != false ตัวแปร $countEndLoop จะตรวจสอบลูปและ save image ที่ quality = 75 คือ default
if ($countEndLoop > 20) {
$bool = false;
$objImage->free();
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$aImgSize = $objImage->getImageSize();
$objImage->resize($aImgSize[0], $aImgSize[1]);
$objImage->save($fileImgDest, $objImage->getImageType());
$objImage->free();
}
}
} else {
SGL::logMessage('case else RVsupportResize :: filesize ' . filesize($fileImgSource) . ' maxsize = ' . $maxSize , PEAR_LOG_DEBUG);
if (is_file($fileImgDest) === false) {
RvsLibs_System::copy(array('-f', $fileImgSource, $fileImgDest));
RvsLibs_System::chmod($fileImgDest, 0755);
}
}
SGL_Error::reset();
return true;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $pixel - Pixel to resize
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return Boolean True
*/
public static function makeResizeByPixel($fileImgSource, $fileImgDest, $pixel, $createThumbnailBaseOn='gd', $options=null)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
SGL_Error::reset();
//$objImage = rvs_Transform::singleton();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
if (count($objImage->valid_driver) == 0) {
return SGL::raiseError(RvsLibs_String::translate('On your server do not have gd library and imagemagick. You cannot generate thumbnail'));
}
$aImgSize = $objImage->getImageSize();
$aPosition = RvsLibs_Image2::getPositionAutoThumb($aImgSize[0], $aImgSize[1], $pixel);
if (isset($aPosition['makeBackgroundImage']) && $aPosition['makeBackgroundImage']) {
if (is_file($fileImgDest) === false) {
RvsLibs_System::copy(array('-f', $fileImgSource, $fileImgDest));
RvsLibs_System::chmod($fileImgDest, 0755);
}
} else {
$objImage->resize($aPosition[0], $aPosition[1], $options);
$objImage->save($fileImgDest, $objImage->getImageType());
$objImage->free();
}
return true;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $new_x - Pixel to resize
* @param Integer $new_y - Pixel to resize
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return Boolean True
*/
public static function resize($fileImgSource, $fileImgDest, $new_x, $new_y, $createThumbnailBaseOn='gd', $options=null)
{
SGL::logMessage(' rvdebug: new_x: ' . $new_x . ' x new_y:' . $new_y, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
SGL_Error::reset();
$quality = isset($options['quality']) ? $options['quality'] : 80;
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
if (count($objImage->valid_driver) == 0) {
return SGL::raiseError(RvsLibs_String::translate('On your server do not have gd library and imagemagick. You cannot generate thumbnail'));
}
//case ไม่ได้ resize
if ($objImage->img_obj->new_x == $new_x && $objImage->img_obj->new_y == $new_y) {
SGL::logMessage('********size not change.**********', PEAR_LOG_INFO);
$objImage->free();
if ($fileImgSource != $fileImgDest) {
$res = RvsLibs_System::copy(array('-f', $fileImgSource, $fileImgDest));
if (SGL::isError($res) == true) {
SGL_Error::pop();
SGL::logMessage('cannot resized to ' . $fileImgDest . '. Please check permission file and folder.', PEAR_LOG_ERR);
return false;
}
}
return true;
}
$res = $objImage->resize($new_x, $new_y, $options);
if (SGL::isError($res) == true) {
return $res;
}
$res = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($res) == true) {
return $res;
}
$objImage->free();
return true;
}
/**
* getAutoFontSize
*
* @param $width
* @param $height
* @return Integer $fontSize
*/
public static function getAutoFontSize($width, $height)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
$fontSize = 0;
//[ In class constructor ]#
// Get size in pixels, must convert to points for GD2.
// Because GD2 assumes 96 pixels per inch and we use more "standard" 72.
$fontSize = ceil(max($width, $height) / 72);
return $fontSize;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param String $strText - Sring watermark set default = ""
* @param String $color - Font color set default = "black"
* @param Integer $rotation - Rotation image set default = "0" degree
* @param String $position - NorthWest, North, NorthEast
* - West, Center,East
* - SouthWest, South, or SouthEast
* @param Integer $marginAdjust
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return mix PEAR error or TRUE on success
*/
public static function makeWaterMarkText2($fileImgSource, $fileImgDest, $strText='', $color='black', $rotation="0", $position="SouthEast", $marginAdjust=5, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$resWatermarkText = $objImage->makeWatermarkText2($strText, $color, $rotation, $position, $marginAdjust);
if (SGL::isError($resWatermarkText) === true) {
return $resWatermarkText;
}
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true) {
return $resSave;
}
$objImage->free();
return true;
}
/**
* makeThumb
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $maxPixelThumb - Max pixel thumb
* @param Integer $border - Border size
* @param String $color - Border color
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return mix PEAR error or TRUE on success
*/
public static function makeThumb($fileImgSource, $fileImgDest, $maxPixelThumb, $border=2, $color='black', $quality= 75, $createThumbnailBaseOn='gd')
{
SGL::logMessage('Source: ' . $fileImgSource, PEAR_LOG_DEBUG);
SGL::logMessage('Dest: ' . $fileImgDest, PEAR_LOG_DEBUG);
//MAKE CROP
$resMakeCrop = RvsLibs_Image2::makeCrop($fileImgSource, $fileImgDest, $maxPixelThumb, $quality, $createThumbnailBaseOn);
if (SGL::isError($resMakeCrop) === true) { return $resMakeCrop; }
$fileImgSource = $fileImgDest;
$resMakeAutoBg = RvsLibs_Image2::makeAutoBg($fileImgSource, $fileImgDest, $maxPixelThumb, $quality, $createThumbnailBaseOn);
if (SGL::isError($resMakeAutoBg) === true) { return $resMakeAutoBg; }
//กรอบสี default ขาว
$resMakeBorderRound = RvsLibs_Image2::makeBorderRound($fileImgSource, $fileImgDest, $maxPixelThumb, $border, $quality, $createThumbnailBaseOn);
if (SGL::isError($resMakeBorderRound) === true) { return $resMakeBorderRound; }
$resMakeBorder = RvsLibs_Image2::makeBorder($fileImgSource, $fileImgDest, $border, $color, $quality, $createThumbnailBaseOn);
if (SGL::isError($resMakeBorder) === true) { return $resMakeBorder; }
$resMakeSquareThumb = RvsLibs_Image2::makeSquareThumb($fileImgSource, $fileImgDest, $maxPixelThumb, $quality, $createThumbnailBaseOn);
if (SGL::isError($resMakeSquareThumb) === true) { return $resMakeSquareThumb; }
return true;
}
/**
* makeCrop
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $maxPixelThumb - Max pixel thumb
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @param array $aSizeCrop = array('w' => {width}, 'h' => '{height}')
* @param array $aPositionXY = array('x' => {position x}, 'y' => '{position y}')
* @return mix PEAR error or TRUE on success
*/
public static function makeCrop($fileImgSource, $fileImgDest, $maxPixelThumb, $quality=75, $createThumbnailBaseOn='gd', $aSizeCrop = array(), $aPositionXY = array())
{
SGL::logMessage('driver:' . $createThumbnailBaseOn . ' ImgSource ' . $fileImgSource, PEAR_LOG_DEBUG);
SGL::logMessage('ImgDest ' . $fileImgDest, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
//auto position ratio
//$objImage = rvs_Transform::singleton();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
if (count($objImage->valid_driver) == 0) {
return SGL::raiseError(RvsLibs_String::translate('On your server do not have gd library and imagemagick. You cannot generate thumbnail'));
}
$aImgSize = $objImage->getImageSize();
$width = isset($aSizeCrop['w']) ? $aSizeCrop['w'] : $aImgSize[0];
$height = isset($aSizeCrop['h']) ? $aSizeCrop['h'] : $aImgSize[1];
$maxPixelThumb = self::_getMaxThum($width, $height);
$aPosition = RvsLibs_Image2::getPositionAutoCropRatio($width, $height, $maxPixelThumb);
$positionX = (isset($aPositionXY['x'])) ? $aPositionXY['x'] : $aPosition[0];
$positionY = (isset($aPositionXY['y'])) ? $aPositionXY['y'] : $aPosition[1];
SGL::logMessage('w=' . $width .' h=' . $height . ' x=' . $positionX . ' y=' . $positionY . ' maxthumb=' . $maxPixelThumb, PEAR_LOG_DEBUG);
$resCrop = $objImage->crop($aPosition[2], $aPosition[3], $positionX, $positionY);
if (SGL::isError($resCrop) === true) { return $resCrop; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return true;
}
public static function customCrop($fileImgSource, $fileImgDest, $aSizeCrop, $quality=75, $createThumbnailBaseOn='gd')
{
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
if (count($objImage->valid_driver) == 0) {
return SGL::raiseError(RvsLibs_String::translate('On your server do not have gd library and imagemagick. You cannot generate thumbnail'));
}
if (!isset($aSizeCrop['w']) || !isset($aSizeCrop['h'])) {
$aImgSize = $objImage->getImageSize();
$aSizeCrop['w'] = $aImgSize[0];
$aSizeCrop['h'] = $aImgSize[1];
}
$positionX = (isset($aSizeCrop['x'])) ? $aSizeCrop['x'] : 0;
$positionY = (isset($aSizeCrop['y'])) ? $aSizeCrop['y'] : 0;
$resCrop = $objImage->crop($aSizeCrop['w'], $aSizeCrop['h'], $positionX, $positionY);
if (SGL::isError($resCrop) === true) { return $resCrop; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return true;
}
/**
* หา maxthumbnail ตามขนาดของรูป
* min = 120
* max = 500
*
* @param $width
* @param $height
* @param $maxPixelThumb
* @return unknown_type
*/
public static function _getMaxThum($width, $height)
{
return $maxThum = (RvsLibs_Image2::isRange($width, $height) == true) ? $width+5 : $height+5;
//return ($maxThum >= 550) ? 550 : $maxThum;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $maxPixelThumb - Max pixel thumb
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return mix PEAR error or TRUE on success
*/
public static function makeAutoBg($fileImgSource, $fileImgDest, $maxPixelThumb, $quality=75, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
//auto thumbs and chk add background
//$objImage = rvs_Transform::singleton();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
$resLoad = $objImage->load($fileImgSource);
SGL::logMessage($objImage->current_driver, PEAR_LOG_DEBUG);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$aImgSize = $objImage->getImageSize();
$aPosition = RvsLibs_Image2::getPositionAutoThumb($aImgSize[0], $aImgSize[1], $maxPixelThumb);
if (isset($aPosition['makeBackgroundImage']) && $aPosition['makeBackgroundImage']) {
$resAddBg = $objImage->addBackground($aImgSize[0], $aImgSize[1], $maxPixelThumb);
if (SGL::isError($resAddBg) === true) { return $resAddBg; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
} else {
$resResize = $objImage->resize($aPosition[0], $aPosition[1]);
if (SGL::isError($resResize) === true) { return $resResize; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
}
return true;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $maxPixelThumb - Max pixel thumb
* @param Integer $border - Border
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return mix PEAR error or TRUE on success
*/
public static function makeBorderRound($fileImgSource, $fileImgDest, $maxPixelThumb, $border, $quality, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
//auto border2 and same w,h
//$objImage = rvs_Transform::singleton();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$aImgSize = $objImage->getImageSize();
$resAddBorderRound = $objImage->addBorderRound($aImgSize[0], $aImgSize[1], $maxPixelThumb, $border);
if (SGL::isError($resAddBorderRound) === true) { return $resAddBorderRound; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return true;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $border - Size boder
* @param String $color - Color border
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return mix PEAR error or TRUE on success
*/
public static function makeBorder($fileImgSource, $fileImgDest, $border=2, $color='gray', $quality, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
//$objImage = rvs_Transform::singleton();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$aImgSize = $objImage->getImageSize();
$resAddBorder = $objImage->addBorder2($border, $color);
if (SGL::isError($resAddBorder) === true) { return $resAddBorder; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return true;
}
/**
* Get size image return type byte.
*
* @param String $fileImgSource - Path file image source
* @return Integer $sizeImgSourceTypeByte - Size image type byte.
*/
public static function getImageSizeTypeByte($fileImgSource)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
$sizeImgSourceTypeByte = 0;
if (is_file($fileImgSource) === true) {
$sizeImgSourceTypeByte = filesize($fileImgSource);
}
return $sizeImgSourceTypeByte;
}
/**
* makeSquareThumb
*
* @param String $fileImgSource - File name image source
* @param String $fileImgDest - File name image destination
* @param Integer $maxPixelThumb - Max pixel thumb
* @param Integer $border - Border
* @param Integer $quality - Quality
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return mix PEAR error or TRUE on success
*/
public static function makeSquareThumb($fileImgSource, $fileImgDest, $maxPixelThumb, $quality=75, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
//$objImage = rvs_Transform::singleton();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$resResize = $objImage->resize($maxPixelThumb, $maxPixelThumb);
if (SGL::isError($resResize) === true) { return $resResize; }
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), $quality);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return true;
}
public static function getExtOfMimeType($mimetype)
{
$extension = '';
switch (RvsLibs_String::strtolower($mimetype)) {
case 'image/png' :
$extension = 'png';
break;
case 'image/jpeg' :
$extension = 'jpg';
break;
case 'image/jpg' :
$extension = 'jpg';
break;
case 'image/gif' :
$extension = 'gif';
break;
case 'image/tiff' :
$extension = 'tif';
break;
}
return $extension;
}
/**
*
* Create vertical mirroring image
*
* Uses a fast rotation algorythm for custom angles
* or lines copy for multiple of 90 degrees
* @param String $fileImgSource -File name image source
* @param String $fileImgDest -File name image destination
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return Mix True on success or PEAR_Error object on error
*/
public static function flip($fileImgSource, $fileImgDest, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
// image processing
$resFlip = $objImage->flip();
if (SGL::isError($resFlip) === true)
return $resFlip;
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true)
return $resSave;
$objImage->free();
return true;
}
/**
*
* Create horizontal mirroring image
*
* Uses a fast rotation algorythm for custom angles
* or lines copy for multiple of 90 degrees
* @param String $fileImgSource -File name image source
* @param String $fileImgDest -File name image destination
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return Mix True on success or PEAR_Error object on error
*/
public static function mirror($fileImgSource, $fileImgDest, $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
// image processing
$resMirror = $objImage->mirror();
if (SGL::isError($resMirror) === true)
return $resMirror;
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true)
return $resSave;
$objImage->free();
return true;
}
/**
*
* Rotates image by the given angle
*
* Uses a fast rotation algorythm for custom angles
* or lines copy for multiple of 90 degrees
* @param String $fileImgSource -File name image source
* @param String $fileImgDest -File name image destination
* @param int $angle -Rotation angle
* @param $options -array(
* 'canvasColor' => array(r ,g, b), named color or #rrggbb
* )
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return Mix True on success or PEAR_Error object on error
*/
public static function rotate($fileImgSource, $fileImgDest, $angle ,$options=array('canvasColor' => array(255,255,255)), $createThumbnailBaseOn='gd')
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
// image processing
$resRotate = $objImage->rotate($angle ,$options);
if (SGL::isError($resRotate) === true)
return $resRotate;
$resSave = $objImage->save($fileImgDest, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true)
return $resSave;
$objImage->free();
return true;
}
/**
*
* @param String $fileImgSource - File name image source
* @param String $createThumbnailBaseOn - Create thumb base on {GD,IM}
* @return array(0 => (int)imageWidth, 1 => (int)imageHeight)
*/
public static function getImageSize($fileImgSource, $createThumbnailBaseOn='gd')
{
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
SGL_Error::reset();
$objImage = rvs_Transform::factory($createThumbnailBaseOn);
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($fileImgSource);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
if (count($objImage->valid_driver) == 0) {
return SGL::raiseError(RvsLibs_String::translate('On your server do not have gd library and imagemagick. You cannot generate thumbnail'));
}
$aImgSize = $objImage->getImageSize();
$objImage->free();
return $aImgSize;
}
public static function autoCropByDimension($soursce, $des, $option)
{
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
SGL_Error::reset();
$objImage = rvs_Transform::factory('gd');
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($soursce);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
if (!isset($option['w']) && !isset($option['h'])) {
$resSave = $objImage->save($des, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true) { SGL_Error::pop(); }
$objImage->free();
return true;
}
if (isset($option['w']) && isset($option['h'])) {
$newDimension = $option;
} else {
$aImgSize = $objImage->getImageSize();
if (!isset($option['w']) && isset($option['h'])) {
$newDimension = RvsLibs_Image2::getPositionAutoThumb($aImgSize[0], $aImgSize[1], $option['h']);
} elseif (isset($option['w']) && !isset($option['h'])) {
$newDimension = RvsLibs_Image2::getPositionAutoThumb($aImgSize[0], $aImgSize[1], $option['w']);
} else {
$newDimension = self::_getNewDimensionByRatio($option, $aImgSize);
}
}
//$objImage->resize($newDimension['w'], $newDimension['h']);
//crop img
$positionX = (isset($option['x'])) ? $option['x'] : 0;
$positionY = (isset($option['y'])) ? $option['y'] : 0;
$resCrop = $objImage->crop($newDimension['w'], $newDimension['h'], $positionX, $positionY);
if (SGL::isError($resCrop) === true) { return $resCrop; }
$resSave = $objImage->save($des, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return true;
}
private static function _getNewDimensionByRatio($newSize, $imgSize){
$aSize = array();
if ($imgSize[0] <= $newSize['w'] && $imgSize[1] > $imgSize[0]) {
//แนวตั้ง
$aSize['w'] = $imgSize[0];
$aSize['h'] = floor(($newSize['h'] / $newSize['w'])*$imgSize[0]);
} elseif ($imgSize[0] <= $newSize['w'] && $imgSize[1] < $imgSize[0]) {
//แนวนอน
$aSize['w'] = floor($imgSize[1]*($newSize['w'] / $newSize['h']));
$aSize['h'] = $imgSize[1];
} elseif ($imgSize[0] > $newSize['w'] && $imgSize[1] < $imgSize[0]) {
//แนวนอน
$aSize['w'] = floor($newSize['w']*$imgSize[1] / $newSize['h']);
$aSize['h'] = $imgSize[1];
} elseif ($imgSize[0] > $newSize['w'] && $imgSize[1] > $imgSize[0]) {
//แนวตั้ง
$aSize['h'] = floor($imgSize[0]*($newSize['h'] / $newSize['w']));
$aSize['w'] = $imgSize[0];
}
SGL::logMessage('new-width'.$aSize['w'].'new-height'.$aSize['h'], PEAR_LOG_ERR);
return $aSize;
}
/**
*
* @param string $soursce : image path
* @param string $des : image save path
* @param array $option : array('maxHeight'=>350,'x'=>0,'y'=>0,'ratio'=>3)
* @return true on success , PEAR error on failed
*/
public static function autoHorizontalCrop($soursce, $des, $option)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
require_once(SGL_LIB_PEAR_DIR . '/Image/rvs_Transform.php');
SGL_Error::reset();
$objImage = rvs_Transform::factory('gd');
if (SGL::isError($objImage) === true) {
return $objImage;
}
$resLoad = $objImage->load($soursce);
if ($resLoad === false) {
SGL_Error::pop();
SGL::logMessage('Cannot load image object.', PEAR_LOG_ERR);
return false;
}
$aImgSize = $objImage->getImageSize();
//crop img
/*
if (floor($aImgSize[0]/$aImgSize[1]) == $option['ratio']) {
$width = floor($option['maxHeight']*$option['ratio']);
$resCrop = $objImage->resize(
floor($option['maxHeight']*$option['ratio'])
, $option['maxHeight']
);
$option['x'] = $positionX;
$option['y'] = $positionY;
$option['x2'] = $positionX+$width;
$option['y2'] = $positionY+$option['maxHeight'];
} else {
*/
$newDimension = self::getHorizontalDimension(
$aImgSize,
$option['maxHeight'],
$option['ratio']
);
$positionX = (isset($option['x'])) ? $option['x'] : 0;
if ($newDimension['h'] > ($aImgSize[1]*2/3)) {
$positionY = 0;
} else {
$positionY = (isset($option['y']))
? $option['y']
: floor($aImgSize[1]/3);
}
$newDimension['h'] = ($newDimension['h'] < 520) ? 520 : $newDimension['h'];
$newDimension['w'] = ($newDimension['w'] > 1440) ? 1440 : $newDimension['w'];
SGL::logMessage('option maxHeight:' . $option['maxHeight'], PEAR_LOG_DEBUG);
SGL::logMessage('newDimension w:' . $newDimension['w'], PEAR_LOG_DEBUG);
SGL::logMessage('newDimension h:' . $newDimension['h'], PEAR_LOG_DEBUG);
$resCrop = $objImage->crop(
$newDimension['w'],
$newDimension['h'],
$positionX,
$positionY
);
$option['x'] = $positionX;
$option['y'] = $positionY;
$option['x2'] = $positionX+$newDimension['w'];
$option['y2'] = $positionY+$newDimension['h'];
//}
if (SGL::isError($resCrop) === true) { return $resCrop; }
$resSave = $objImage->save($des, $objImage->getImageType(), 100);
if (SGL::isError($resSave) === true) { return $resSave; }
$objImage->free();
return $option;
}
public static function getHorizontalDimension($aImgSize, $maxHeight, $ratio=3)
{
SGL::logMessage('aImgSize[0]' . $aImgSize[0], PEAR_LOG_DEBUG);
SGL::logMessage('aImgSize[1]' . $aImgSize[1], PEAR_LOG_DEBUG);
SGL::logMessage('maxHeight:' . $maxHeight, PEAR_LOG_DEBUG);
//w = $aImgSize[0] h = $aImgSize[1]
if ($ratio == 0) {
return SGL::raiseError(RvsLibs_String::translate('parameter 3 cannot be zero'));
}
$aSize = array();
if ($aImgSize[0] == $maxHeight*$ratio) {
$aSize['w'] = $aImgSize[0];
$aSize['h'] = $maxHeight;
///resize by
} elseif ($aImgSize[0] > $maxHeight*$ratio) {
SGL::logMessage('crop by height', PEAR_LOG_DEBUG);
///crop by height
if ($aImgSize[1] >= $maxHeight) {
$aSize['w'] = floor($maxHeight*$ratio);
$aSize['h'] = $maxHeight;
} else {
$aSize['w'] = floor($aImgSize[1]*$ratio);
$aSize['h'] = $aImgSize[1];
}
} else {
SGL::logMessage('crop by width', PEAR_LOG_DEBUG);
///crop by width
//$height = floor($aImgSize[0]/$ratio);
//$aSize['h'] = ($height > $maxHeight) ? $height : $maxHeight;
//$aSize['w'] = $aImgSize[0];
$aSize['w'] = $aImgSize[0];
$aSize['h'] = floor($aImgSize[0]/$ratio);
SGL::logMessage('crop w:' . $aSize['w'], PEAR_LOG_DEBUG);
SGL::logMessage('crop h:' . $aSize['h'], PEAR_LOG_DEBUG);
}
return $aSize;
}
}
}
?>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez