getArr($name); if (is_file($file) && file_exists($file)){ list ($width, $height) = getimagesize($file); $this->_initTables(); $this->saveOption($imgArr['id'], 'width', strval($width)); $this->saveOption($imgArr['id'], 'height', strval($height)); } return $name; } return false; } function saveOption($id_file, $name, $value) { if ($this->hasOption($id_file, $name)) { $this->tblFileOpt->update( compact('value'), '`id_file` = ' . $this->db->quote($id_file, 'integer') . ' ' . 'AND `name` = ' . $this->db->quote($name, 'text') . ' ' ); } else { $this->tblFileOpt->insert(compact('id_file', 'name', 'value')); } return $this; } function hasOption($id_file, $name) { $sql = 'SELECT 1 ' . 'FROM `' . $this->tblFileOpt->table . '` ' . 'WHERE 1 ' . 'AND `id_file` = ' . $this->db->quote($id_file, 'integer') . ' ' . 'AND `name` = ' . $this->db->quote($name, 'text') . ' '; $res = $this->db->queryOne($sql); return '1' === $res; } function resize($fileName, $iconWidth, $iconHeight, $force = false) { if ( !$this->nameExists($fileName) and !is_file(WWW_PATH . '/image/' . $fileName)) { return false; } $iconWidth = intval($iconWidth); $iconHeight = intval($iconHeight); $iconName = $this->getIconName($fileName, $iconWidth, $iconHeight, $force ); if ( !$this->nameExists($iconName) ) { if (is_file(WWW_PATH . '/image/' . $fileName)) { $tempnamFile = '/tmp/' . rand(100000000, 999999999); copy(WWW_PATH.'/image/' . $fileName, $tempnamFile); $name_ext = explode('.', $fileName); $fileArr = array( 'title' => $fileName, 'type' => 'image/' . $name_ext[1] ); $iconTitle = $this->getIconName($fileArr['title'], $iconWidth, $iconHeight ); } else { $tempnamFile = $this->saveFS('tmp/', $fileName); $fileArr = $this->getArr($fileName); $iconTitle = $this->getIconName($fileArr['title'], $iconWidth, $iconHeight ); } $iconDir = $this->_validateDir('tmp/'); $tempnamIcon = tempnam($iconDir, $iconName); list($oldWidth, $oldHeight, $type, $attr) = getimagesize($tempnamFile); if ($force){ new resizer($tempnamFile, $iconWidth, $iconHeight, $tempnamIcon); }elseif ( ($iconWidth > 0 && $oldWidth >= $iconWidth) || ($oldHeight >= $iconHeight && $iconHeight > 0) ){ new resizer($tempnamFile, $iconWidth, $iconHeight, $tempnamIcon); }else { copy($tempnamFile, $tempnamIcon); } $this->insert($iconTitle, $tempnamIcon, $fileArr['type'], $iconName); unlink($tempnamFile); unlink($tempnamIcon); } return $iconName; } function fromIconName($iconName) { $force = false; if ( $xpos = strpos($iconName, 'x') ){ $force = false; } elseif ($xpos = strpos($iconName, 'X')) { $force = true; } if (false === $xpos ) { $arr = array( 'file' => $iconName, 'width' => 0, 'height' => 0 ); } else { $pathParts = pathinfo($iconName); $nameWWH = substr($iconName, 0, - (1 + strlen($pathParts['extension']))); $nameLen = strrpos($nameWWH, '_'); $name = substr($nameWWH, 0, $nameLen) . '.' . $pathParts['extension']; $wh = substr($nameWWH, $nameLen + 1); list($w, $h) = explode(ImageDB::getIconNameSeparator($force), $wh); $arr = array( 'file' => $name, 'width' => intval($w), 'height' => intval($h) ); } $arr['force'] = $force; return $arr; } function getIconNameSeparator($force) { if ($force) { return 'X'; } else { return 'x'; } } function getIconName($fileName, $width, $height, $force = false) { $db = S_db2::getInstance(); $aOpts = $db->queryAll( 'SELECT o.name, o.value ' . 'FROM mn_zz_File f ' . 'INNER JOIN mn_zz_FileOpt o ' . ' ON f.id = o.id_file ' . 'WHERE f.name = ' . $db->quote($fileName) ); $resize = false; foreach ($aOpts as $aOpt) { if (${$aOpt['name']} < $aOpt['value']) { $resize = true; break; } } if (!$resize) { return $fileName; } $width = intval($width); $height = intval($height); $path_parts = pathinfo((string) $fileName); if (strpos($fileName, '.') === false) { $name = $path_parts['basename']; $iconName = $name.'_'.$width. ImageDB::getIconNameSeparator($force) .$height; } else { $extLen = strlen( $path_parts['extension']); $name = substr($path_parts['basename'], 0, -($extLen + 1)); $iconName = $name . '_' . $width . ImageDB::getIconNameSeparator($force) . $height . '.' . $path_parts['extension']; } return $iconName; } function _delIcons($id, $field = 'name') { $fileArr = $this->getArr($id, $field); if (!($fileArr['id'] > 0)) { return false; } $iconName = $this->getIconName($fileArr['name'], 0, 0); $iconName = str_replace('0x0', '%', $iconName); $table = 'tbl' . $this->fileTable; $table = $this->db->$table; $sql = 'SELECT * FROM `' . $table . '` WHERE `name` LIKE \'' . $this->db->escape($iconName) . '\''; $list = $this->db->queryAll($sql); foreach ($list as $arr) { parent::delete($arr['id'], 'id'); } return true; } function delete($id, $field = 'name') { $this->_delIcons($id, $field); return parent::delete($id, $field);/**/ } } class resizer { var $quality = 75; var $maxSize = 214700; function _testForExternalResizer() { $result = shell_exec(IMAGE_MAGICK_PATH . ' -version'); if (!empty($result)) { return 'IM'; } if (extension_loaded('gd')) { return 'GD'; } return false; } function _validateSize($size) { $size = intval($size); if ( empty($size) ) { $size = $this->maxSize; } return $size; } function resizer($fullPath, $width, $height, $iconName) { $width = $this->_validateSize($width); $height = $this->_validateSize($height); $externalResizer = $this->_testForExternalResizer(); switch ($externalResizer){ case 'IM': $this->_resizeIM($fullPath, $width, $height, $iconName); break; default: $this->_resizeGD($fullPath, $width, $height, $iconName); break; } } function _getNewSize($fullPath, $width, $height) { list($oldWidth, $oldHeight) = getimagesize($fullPath); if (($oldWidth / $oldHeight) >= ($width / $height)) { $newx = $width; $widn = $oldWidth/$width ; $newy = floor($oldHeight / $widn); } else { $widn = $oldHeight/$height; $newx = floor( $oldWidth / $widn); $newy = $height; } return array('width' => $newx, 'height' => $newy); } function _resizeGD($fullName, $width, $height, $iconFullName) { $size = $this->_getNewSize($fullName, $width, $height); $newx = $size['width']; $newy = $size['height']; list($oldWidth, $oldHeight, $imgType) = getimagesize($fullName); $destimg = imagecreatetruecolor($newx, $newy); if ($imgType == 1){ $sourceimg = imagecreatefromgif ($fullName); } elseif ($imgType == 2){ $sourceimg = imagecreatefromjpeg ($fullName); } elseif ($imgType == 3){ $sourceimg = imagecreatefrompng ($fullName); } @imagecopyresampled($destimg, $sourceimg, 0, 0, 0, 0, $newx, $newy, $oldWidth, $oldHeight); if ($imgType == 1) { imagegif($destimg, $iconFullName, $this->quality); } elseif ($imgType == 2) { imagejpeg($destimg, $iconFullName, $this->quality); } elseif ($imgType == 3) { imagepng($destimg, $iconFullName, $this->quality); } return true; } function _resizeIM($fullName, $width, $height, $iconFullName) { $r = IMAGE_MAGICK_PATH . " -size " . $width . 'x' . $height . " '" . $fullName . "' -resize " . $width . 'x' . $height . " +profile \"*\" \"" . $iconFullName . "\""; shell_exec($r); `$r`; return true; } } function image_db($name, $width = 0, $height = 0, $force = false) { $width = intval($width); $height = intval($height); if ($width || $height) { $name = ImageDB::getIconName($name, $width, $height, $force); } return IMAGE_DB_URL.'/'.rawurlencode($name); } function image_db_full($name, $width = 0, $height = 0, $force = false) { static $ImageDB; if (!isset($ImageDB)) { $ImageDB = new ImageDB(); } if ($width || $height){ $iconName = $ImageDB->resize($name, $width, $height, $force); } else { $iconName = $name; } $imgArr = $ImageDB->getArr($iconName); $out = ' src="' . image_db($name, $width, $height, $force) . '" '; if ($imgArr['opt']['width']){ $out .= 'width="' . $imgArr['opt']['width'] . '" '; } if ($imgArr['opt']['height']){ $out .= 'height="' . $imgArr['opt']['height'] . '" '; } return $out; }