oxutilspic.php

Go to the documentation of this file.
00001 <?php
00002 
00006 require_once getShopBasePath() . "core/utils/oxpicgenerator.php";
00007 
00011 class oxUtilsPic extends oxSuperCfg
00012 {
00013 
00019     protected $_aImageTypes = array("GIF" => IMAGETYPE_GIF, "JPG" => IMAGETYPE_JPEG, "PNG" => IMAGETYPE_PNG, "JPEG" => IMAGETYPE_JPEG);
00020 
00031     public function resizeImage($sSrc, $sTarget, $iDesiredWidth, $iDesiredHeight)
00032     {
00033         $blResize = false;
00034 
00035         // use this GD Version
00036         if (($iUseGDVersion = getGdVersion()) && function_exists('imagecreate') &&
00037             file_exists($sSrc) && ($aImageInfo = @getimagesize($sSrc))
00038         ) {
00039 
00040             $myConfig = $this->getConfig();
00041             list($iWidth, $iHeight) = calcImageSize($iDesiredWidth, $iDesiredHeight, $aImageInfo[0], $aImageInfo[1]);
00042             $blResize = $this->_resize($aImageInfo, $sSrc, null, $sTarget, $iWidth, $iHeight, $iUseGDVersion, $myConfig->getConfigParam('blDisableTouch'), $myConfig->getConfigParam('sDefaultImageQuality'));
00043         }
00044 
00045         return $blResize;
00046     }
00047 
00048 
00059     public function safePictureDelete($sPicName, $sAbsDynImageDir, $sTable, $sField)
00060     {
00061         $blDelete = false;
00062         if ($this->_isPicDeletable($sPicName, $sTable, $sField)) {
00063             $blDelete = $this->_deletePicture($sPicName, $sAbsDynImageDir);
00064         }
00065 
00066         return $blDelete;
00067     }
00068 
00077     protected function _deletePicture($sPicName, $sAbsDynImageDir)
00078     {
00079         $blDeleted = false;
00080         $myConfig = $this->getConfig();
00081 
00082         if (!$myConfig->isDemoShop() && (strpos($sPicName, 'nopic.jpg') === false ||
00083                                          strpos($sPicName, 'nopic_ico.jpg') === false)
00084         ) {
00085 
00086             $sFile = "$sAbsDynImageDir/$sPicName";
00087 
00088             if (file_exists($sFile) && is_file($sFile)) {
00089                 $blDeleted = unlink($sFile);
00090             }
00091 
00092             if (!$myConfig->getConfigParam('sAltImageUrl')) {
00093                 // deleting various size generated images
00094                 $sGenPath = str_replace('/master/', '/generated/', $sAbsDynImageDir);
00095                 $aFiles = glob("{$sGenPath}*/{$sPicName}");
00096                 if (is_array($aFiles)) {
00097                     foreach ($aFiles as $sFile) {
00098                         $blDeleted = unlink($sFile);
00099                     }
00100                 }
00101             }
00102         }
00103 
00104         return $blDeleted;
00105     }
00106 
00107 
00118     protected function _isPicDeletable($sPicName, $sTable, $sField)
00119     {
00120         if (!$sPicName || strpos($sPicName, 'nopic.jpg') !== false || strpos($sPicName, 'nopic_ico.jpg') !== false) {
00121             return false;
00122         }
00123 
00124         $oDb = oxDb::getDb();
00125         $iCountUsed = $oDb->getOne("select count(*) from $sTable where $sField = " . $oDb->quote($sPicName) . " group by $sField ", false, false);
00126 
00127         return $iCountUsed > 1 ? false : true;
00128     }
00129 
00143     public function overwritePic($oObject, $sPicTable, $sPicField, $sPicType, $sPicDir, $aParams, $sAbsDynImageDir)
00144     {
00145         $blDelete = false;
00146         $sPic = $sPicTable . '__' . $sPicField;
00147         if (isset($oObject->{$sPic}) &&
00148             ($_FILES['myfile']['size'][$sPicType . '@' . $sPic] > 0 || $aParams[$sPic] != $oObject->{$sPic}->value)
00149         ) {
00150 
00151             $sImgDir = $sAbsDynImageDir . oxRegistry::get("oxUtilsFile")->getImageDirByType($sPicType);
00152             $blDelete = $this->safePictureDelete($oObject->{$sPic}->value, $sImgDir, $sPicTable, $sPicField);
00153         }
00154 
00155         return $blDelete;
00156     }
00157 
00172     protected function _resizeGif($sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch)
00173     {
00174         return resizeGif($sSrc, $sTarget, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer, $blDisableTouch);
00175     }
00176 
00192     protected function _resize($aImageInfo, $sSrc, $hDestinationImage, $sTarget, $iNewWidth, $iNewHeight, $iGdVer, $blDisableTouch, $iDefQuality)
00193     {
00194         startProfile("PICTURE_RESIZE");
00195 
00196         $blSuccess = false;
00197         switch ($aImageInfo[2]) { //Image type
00198             case ($this->_aImageTypes["GIF"]):
00199                 //php does not process gifs until 7th July 2004 (see lzh licensing)
00200                 if (function_exists("imagegif")) {
00201                     $blSuccess = resizeGif($sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo[0], $aImageInfo[1], $iGdVer);
00202                 }
00203                 break;
00204             case ($this->_aImageTypes["JPEG"]):
00205             case ($this->_aImageTypes["JPG"]):
00206                 $blSuccess = resizeJpeg($sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage, $iDefQuality);
00207                 break;
00208             case ($this->_aImageTypes["PNG"]):
00209                 $blSuccess = resizePng($sSrc, $sTarget, $iNewWidth, $iNewHeight, $aImageInfo, $iGdVer, $hDestinationImage);
00210                 break;
00211         }
00212 
00213         if ($blSuccess && !$blDisableTouch) {
00214             @touch($sTarget);
00215         }
00216 
00217         stopProfile("PICTURE_RESIZE");
00218 
00219         return $blSuccess;
00220     }
00221 
00236     protected function _copyAlteredImage($sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer, $blDisableTouch)
00237     {
00238         $blSuccess = copyAlteredImage($sDestinationImage, $sSourceImage, $iNewWidth, $iNewHeight, $aImageInfo, $sTarget, $iGdVer);
00239         if (!$blDisableTouch && $blSuccess) {
00240             @touch($sTarget);
00241         }
00242 
00243         return $blSuccess;
00244     }
00245 }