Čuchám z toho, že chceš urobiť thumbnail obrázku. Ak som trafil, skús použiť toto:
Kód:
<?php
class NejakSiTuTrieduPomenuj
{
private $img;
private $filename;
public function __construct($filename,$filetype='jpg')
{
if (!file_exists($filename)) {
return false;
}
if (!$this->img = imagecreatefromjpeg($filename)) {
return false;
}
$this->filename = $filename;
}
public function thumbnail($new_w=640,$new_h=480)
{
$old_w=imageSX($this->img);
$old_h=imageSY($this->img);
$pomer = $old_h/$old_w;
if ($old_w<=$new_w and $old_h<=$new_h) {
$thumb_w = $old_w;
$thumb_h = $old_h;
} else if ($old_w>$new_w) {
$thumb_w = $new_w;
$thumb_h = intval(($new_w/$old_w)*$old_h);
}
if ($thumb_h>$new_h) {
$thumb_h = $new_h;
$thumb_w = intval(($new_h/$old_h)*$old_w);
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$this->img,0,0,0,0,$thumb_w,$thumb_h,$old_w,$old_h);
imagedestroy($this->img);
$this->img = $dst_img;
return true;
}
public function save($filename = null)
{
if (is_null($filename)) {
$filename = $this->filename;
}
if (imagejpeg($this->img,$filename)) {
return true;
}
return false;
}
public function __destruct()
{
imagedestroy($this->img);
}
public function print_jpeg()
{
imagejpeg($this->img);
}
}
Použitie:
Kód:
$thumb1 = new NejakSiTuTrieduPomenuj($filename);
$thumb1->thumbnail(640,480);
$thumb1->save($nejakacesta);
Tá časť, čo zaujíma teba, je tu
Kód:
$old_w=imageSX($this->img);
$old_h=imageSY($this->img);
$pomer = $old_h/$old_w;
if ($old_w<=$new_w and $old_h<=$new_h) {
$thumb_w = $old_w;
$thumb_h = $old_h;
} else if ($old_w>$new_w) {
$thumb_w = $new_w;
$thumb_h = intval(($new_w/$old_w)*$old_h);
}
if ($thumb_h>$new_h) {
$thumb_h = $new_h;
$thumb_w = intval(($new_h/$old_h)*$old_w);
}
V tej triede zatiaľ dohromady nič nie je, zatiaľ som ani nič nepotreboval okrem toho, čo tam je.