Ég veit ekki alveg hvað er að en þetta *gæti* verið ástæðan:
$src_img=ImageCreateFrompng($cfg_fullsizepics_path."/".$entry);
Er þetta ekki væntanlega jpeg myndir?
En annars þá er þetta hérna nokkuð sem var sent inná spjallþráð á netinu af ‘Mau’ (sést líka í kóðanum):
// Class created by Carl "Mau" Vondrick
class Thumb {
// * Public Properities * \\
public $path,$scale=1;
public $cache,$cache_path,$cache_prefix; // Cache properties
public $real_width,$real_height;
public $new_width,$new_height;
public $image_r,$image_tr; /* Image Resources:
$image_r = regular image resource
$image_tr = thumb image resource
*/
// * Public Methods * \\
public function __construct($path) {
// Error checking
if (!file_exists($path)) throw new Exception("File $path does not exist");
$this->path = $path;
list($this->real_width,$this->real_height) = getimagesize($this->path);
}
public function __destruct() {
if (is_resource($this->image_r)) imagedestroy($this->image_r);
if (is_resource($this->image_tr)) imagedestroy($this->imagetr);
}
// Method: set_target
// Calculates percent based off of target
// $dem, string, either 'h' or 'w'
// $to, int, above 0
public function set_target($dem,$to) {
// Error checking
if ($dem != 'h' && $dem != 'w') throw new Exception("Invalid dimension");
if (!is_numeric($to) || $to <= 0) throw new Exception("To must be an int above 0.");
// calculate scale based off of target
if ($dem == 'h') $this->scale = $to / $this->real_height;
else $this->scale = $to / $this->real_width;
}
// Method: set_percent
// Sets the percent to be predefined
// $to, int, above 0
public function set_percent($to) {
// Error checking
if (!is_numeric($to) || $to <= 0) throw new Exception("To must be an int above 0");
$this->scale = $to;
}
// Method: set_max
// Adjusts the scale to the max
// $dem, string, either 'h' or 'w'
// $to, int, the max
public function set_max($dem,$to) {
// Error checking
if ($dem != 'h' && $dem != 'w') throw new Exception("Invalid dimension");
if (!is_numeric($to) || $to <= 0) throw new Exception("To must be an int above 0.");
$this->calculate_new_dems();
if ($dem == 'h' && $to < $this->new_height) $this->scale = $to / $this->real_height;
elseif ($dem == 'w' && $to < $this->new_width) $this->scale = $to / $this->real_width;
}
// Method no_huge
// Adjusts the scale so it does not go above 100%
// No atr
public function no_huge() {
if ($this->scale > 1) $this->scale = 1;
}
// Method output
// Outputs the image
// $cache, bool, wether to pull from cache if possible
public function output($cache=true) {
if (headers_sent()) throw new Exception("Headers already sent. Not sending image binary.");
header("Content-type: image/jpeg");
if ($this->scale == 1) { // image does not need to be scaled, so let's just output
print file_get_contents($this->path);
}
else {
if (!$this->cache_exists()) $this->create_image();
elseif (!$cache) $this->create_image();
print file_get_contents($this->cache_path.'/'.$this->get_cache_lookup());
}
}
// * Protected Methods * \\
protected function create_image() {
$this->calculate_new_dems();
$this->image_tr = imagecreatetruecolor($this->new_width, $this->new_height);
$this->image_r = imagecreatefromjpeg($this->path);
imagecopyresampled($this->image_tr,$this->image_r,0, 0, 0, 0,$this->new_width,$this->new_height,$this->real_width,$this->real_height);
imagejpeg($this->image_tr,$this->cache_path.'/'.$this->get_cache_lookup());
imagedestroy($this->image_tr);
imagedestroy($this->image_r);
}
protected function calculate_new_dems() {
$this->new_height = $this->real_height * $this->scale;
$this->new_width = $this->real_width * $this->scale;
}
protected function cache_exists() {
return file_exists($this->cache_path.'/'.$this->get_cache_lookup());
}
protected function get_cache_lookup() {
return sha1($this->path.' '.$this->scale);
}
}
Og dæmi um notkun:
try {
$thumb = new Thumb("../clients/sam/images/sam.jpg");
$thumb->set_target('h',500); // Tries to make the image with a height of 500
$thumb->set_max('h',400); // Makes sure that the image height is no greater than 400
$thumb->no_huge(); // Scales image to 100% if it is above 100%
$thumb->set_percent(2); // Sets the image to 200%
$thumb->cache_path = $_SERVER['DOCUMENT_ROOT']; // no trailing slash
$thumb->output(true);
}
catch (Exception $ex) {
print '<strong>Sorry! This page cannot continue processing. The exception \'Exception\' was caught.</strong><br /><em>Message: </em> '.$ex->getMessage();
die;
}