/**
* Clasa pentru manipularea imaginilor
*/
class ThumbnailCache
{
var $errmsg = "";
var $error = false;
var $format = "";
var $file = "";
var $max_width = 0;
var $max_height = 0;
var $percent = 0;
var $jpeg_quality = 75;
function ThumbnailCache($file, $max_width = 0, $max_height = 0, $percent = 0)
{
if (!file_exists($file))
{
$this->errmsg = "File doesn't exists";
$this->error = true;
}
else if (!is_readable($file))
{
$this->errmsg = "File is not readable";
$this->error = true;
}
if (strstr(strtolower($file), ".gif"))
{
$this->format = "GIF";
}
else if (strstr(strtolower($file), ".jpg") || strstr(strtolower($file), ".jpeg"))
{
$this->format = "JPEG";
}
else if (strstr(strtolower($file), ".png"))
{
$this->format = "PNG";
}
else
{
$this->errmsg = "Unknown file format";
$this->error = true;
}
if ($max_width == 0 && $max_height == 0 && $percent == 0)
{
$percent = 100;
}
$this->max_width = $max_width;
$this->max_height = $max_height;
$this->percent = $percent;
$this->file = $file;
}
function calc_width($width, $height)
{
$new_width = $this->max_width;
$new_wp = (100 * $new_width) / $width;
$new_height = ($height * $new_wp) / 100;
return array($new_width, $new_height);
}
function calc_height($width, $height)
{
$new_height = $this->max_height;
$new_hp = (100 * $new_height) / $height;
$new_width = ($width * $new_hp) / 100;
return array($new_width, $new_height);
}
function calc_percent($width, $height)
{
$new_width = ($width * $this->percent) / 100;
$new_height = ($height * $this->percent) / 100;
return array($new_width, $new_height);
}
function return_value($array)
{
$array[0] = intval($array[0]);
$array[1] = intval($array[1]);
return $array;
}
function calc_image_size($width, $height)
{
$new_size = array($width, $height);
if ($this->max_width > 0 && $width > $this->max_width)
{
$new_size = $this->calc_width($width, $height);
if ($this->max_height > 0 && $new_size[1] > $this->max_height)
{
$new_size = $this->calc_height($new_size[0], $new_size[1]);
}
return $this->return_value($new_size);
}
if ($this->max_height > 0 && $height > $this->max_height)
{
$new_size = $this->calc_height($width, $height);
return $this->return_value($new_size);
}
if ($this->percent > 0)
{
$new_size = $this->calc_percent($width, $height);
return $this->return_value($new_size);
}
return $new_size;
}
function show_error_image()
{
header("Content-type: image/png");
$err_img = ImageCreate(220, 25);
$bg_color = ImageColorAllocate($err_img, 0, 0, 0);
$fg_color1 = ImageColorAllocate($err_img, 255, 255, 255);
$fg_color2 = ImageColorAllocate($err_img, 255, 0, 0);
ImageString($err_img, 3, 6, 6, "ERROR:", $fg_color2);
ImageString($err_img, 3, 55, 6, $this->errmsg, $fg_color1);
ImagePng($err_img);
ImageDestroy($err_img);
}
function show($name = "")
{
global $config;
if ($this->error)
{
$this->show_error_image();
return;
}
$size = GetImageSize($this->file);
$new_size = $this->calc_image_size($size[0], $size[1]);
#
# Requires GD 2.0.1 (PHP >= 4.0.6)
#
if (function_exists("ImageCreateTrueColor"))
{
if ( $this->format == "GIF" )
$new_image = ImageCreate($new_size[0], $new_size[1]);
else
$new_image = ImageCreateTrueColor($new_size[0], $new_size[1]);
//$new_image = ImageCreateTrueColor($new_size[0], $new_size[1]);
}
else
{
$new_image = ImageCreate($new_size[0], $new_size[1]);
}
switch ($this->format)
{
case "GIF":
$old_image = ImageCreateFromGif($this->file);
break;
case "JPEG":
$old_image = ImageCreateFromJpeg($this->file);
break;
case "PNG":
$old_image = ImageCreateFromPng($this->file);
break;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
if ( ($this->format == "GIF") || ($this->format == "PNG") ) {
$trnprt_indx = imagecolortransparent($old_image);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($old_image, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($new_image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($new_image, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($new_image, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($this->format == "PNG") {
// Turn off transparency blending (temporarily)
imagealphablending($new_image, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($new_image, 0, 0, $color);
// Restore transparency blending
imagesavealpha($new_image, true);
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]);
switch ($this->format)
{
case "GIF":
if (!empty($name))
{
ImageGif($new_image, $name);
@eval("chmod('{$name}', '{$config['rights_file']}');");
}
else
{
header("Content-type: image/gif");
ImageGif($new_image);
}
break;
case "JPEG":
if (!empty($name))
{
ImageJpeg($new_image, $name);
@eval("chmod('{$name}', '{$config['rights_file']}');");
}
else
{
header("Content-type: image/jpeg");
ImageJpeg($new_image, '', $this->jpeg_quality);
}
break;
case "PNG":
if (!empty($name))
{
ImagePng($new_image, $name);
@eval("chmod('{$name}', '{$config['rights_file']}');");
}
else
{
header("Content-type: image/png");
ImagePng($new_image);
}
break;
}
ImageDestroy($new_image);
ImageDestroy($old_image);
return;
}
function save($name)
{
$this->show($name);
}
/**
* Returneaza un string cu calea imaginii folosindu-se de cache
*
*/
function show_from_cache()
{
global $config;
$pathinfo = pathinfo($this->file);
// verificam daca exista directorul de cache
if (!is_dir("{$pathinfo['dirname']}/cache"))
{
@mkdir("{$pathinfo['dirname']}/cache");
@eval("chmod('{$pathinfo['dirname']}/cache', {$config['rights_dir']});");
}
$basename = basename($pathinfo['basename'], ".{$pathinfo['extension']}");
$size = GetImageSize($this->file);
$new_size = $this->calc_image_size($size[0], $size[1]);
if($new_size[0] >= $size[0] || $new_size[1] >= $size[1])
{
//nu avem nevoie sa facem operatii pe imagine
return "{$basename}.{$pathinfo['extension']}";
}
else
{
$cache_path = "{$pathinfo['dirname']}/cache/{$basename}_{$new_size[0]}_{$new_size[1]}.{$pathinfo['extension']}";
$rchache_path = "cache/{$basename}_{$new_size[0]}_{$new_size[1]}.{$pathinfo['extension']}";
if(!file_exists($cache_path))
{
$this->show($cache_path);
}
elseif(filemtime($this->file) > filemtime($cache_path))
{
$this->show($cache_path);
}
return $rchache_path;
}
}
}
?>
/**
* trims text to a space then adds ellipses if desired
* @param string $input text to trim
* @param int $length in characters to trim to
* @param bool $ellipses if ellipses (...) are to be added
* @param bool $strip_html if html tags are to be stripped
* @return string
*/
function trim_text($input, $length, $ellipses = true, $strip_html = true) {
//strip tags, if desired
if ($strip_html) {
$input = strip_tags($input);
}
//no need to trim, already shorter than trim length
if (strlen($input) <= $length) {
return $input;
}
//find last space within length
$last_space = strrpos(substr($input, 0, $length), ' ');
$trimmed_text = substr($input, 0, $last_space);
//add ellipses (...)
if ($ellipses) {
$trimmed_text .= '...';
}
return $trimmed_text;
}
function sluggify($url)
{
# Prep string with some basic normalization
$url = strtolower($url);
$url = strip_tags($url);
$url = stripslashes($url);
$url = html_entity_decode($url);
# Remove quotes (can't, etc.)
$url = str_replace('\'', '', $url);
# Replace non-alpha numeric with hyphens
$match = '/[^a-z0-9]+/';
$replace = '-';
$url = preg_replace($match, $replace, $url);
$url = trim($url, '-');
return $url;
}
?>
Fatal error: Uncaught Error: Non-static method MyActiveRecord::Create() cannot be called statically in /home/pvcadmin1/public_html/php/lib/myactiverecord.php:622
Stack trace:
#0 /home/pvcadmin1/public_html/php/frontendController.php(394): MyActiveRecord::FindBySql()
#1 /home/pvcadmin1/public_html/index.php(36): controller->blog()
#2 {main}
thrown in /home/pvcadmin1/public_html/php/lib/myactiverecord.php on line 622