getArr($name); if (is_file($file) && file_exists($file)){ list($width, $height, $type, $attr) = getimagesize($file); $this->_initTables(); $optArr = array('id_file' => $imgArr['id'], 'name' => 'width', 'value' => strval($width), ); $this->tblFileOpt->insert($optArr); $optArr = array('id_file' => $imgArr['id'], 'name' => 'height', 'value' => strval($height), ); $this->tblFileOpt->insert($optArr); } return $name; }else { return false; }; } 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){ $resizer = new resizer($tempnamFile, $iconWidth, $iconHeight, $tempnamIcon); }elseif ( ($iconWidth > 0 && $oldWidth >= $iconWidth) || ($oldHeight >= $iconHeight && $iconHeight > 0) ){ $resizer = 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) { $arr = array( 'file' => $iconName, 'width' => 0, 'height' => 0, ); if (false !== strpos($iconName, 'x')) { preg_match('/.*_(\d+)x(\d+)\..*/', $iconName, $matches); $width = intval(@$matches[1]); $height = intval(@$matches[2]); if ($width && $height) { $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); $arr = array( 'file' => $name, 'width' => intval($width), 'height' => intval($height), ); } } 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 {$db->tblzz_File} f INNER JOIN {$db->tblzz_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; } } 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 '$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() { // return 'GD'; $result = `which convert`; 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, $type, $attr) = 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, $attr) = getimagesize($fullName); $destimg = imagecreatetruecolor($newx, $newy); if ($imgType == 1){ $sourceimg = imagecreatefromgif ($fullName); } elseif ($imgType == 2){ $sourceimg = imagecreatefromjpeg ($fullName); } elseif ($imgType == 3){ $sourceimg = imagecreatefrompng ($fullName); } // var_dump($imgType); // var_dump($sourceimg); //// vdie($sourceimg); if($gdver == 1){ imagecopyresized($destimg, $sourceimg, 0, 0, 0, 0, $newx, $newy, $oldWidth, $oldHeight); }else{ @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 = "convert -size {$width}x{$height} " . escapeshellarg($fullName) . " -resize {$width}x{$height} +profile \"*\" " . escapeshellarg($iconFullName); // $out = shell_exec ($r); `$r`; return true; } }*/ /* example */ function image_db($name, $width = 0, $height = 0, $force = false) { $width = intval($width); $height = intval($height); if ($width || $height){ static $ImageDB; if (!isset($ImageDB)){ $ImageDB = new ImageDB(); } $name = $ImageDB->resize($name, $width, $height, $force); // $name = ImageDB::getIconName($name, $width, $height, $force); } return IMAGE_DB_URL.'/'.urlencode($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; } ?>