Hi,
I wrote a classFile to handle files that looks like this:
class classfile {
//path_parths is an array, use it this way
//path_parts["filename"] -> filename
//path_parts["dirname"] -> directory
//path_parts["extension"]
//path_parts["basename"]
protected $path_parts;
private $fullpath;
function __construct($filename){
if(file_exists($filename)){
$this->fullpath = $filename;
$this->path_parts= pathinfo($this->fullpath);
}
else{
die("File or Folder doesn't exist");
}
}
function move($destination){
if (file_exists($destination)){
rename($this->fullpath, $destination . "/" . $this->getfilename());
//Update properties
$this->setfullpath($destination . $this->getfilename());
$this->setpath_parts();
}
else{
die("The folder specified doesn't exist");
}
}
function copy($destination){
copy($this->fullpath, $destination);
}
function getpath(){
return $this->path_parts["dirname"];
}
function getfilename(){
return $this->path_parts["basename"];
}
function getextension(){
return $this->path_parts["extension"];
}
function getfullpath(){
return $this->fullpath;
}
function setfullpath($path){
$this->fullpath = $path;
}
function setpath_parts(){
$this->path_parts = pathinfo($this->fullpath);
}
function __destruct() {
;
}
}
?>
Then I extended it with a class to handle images that looks like this:
include 'c:/wamp/www/classfile.php';
class classimage extends classfile{
var $image;
var $image_info;
var $image_type;
function __construct($filename){
parent::__construct($filename);
$this->image_info = getimagesize(parent::getfullpath());
$this->image_type = $this->image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg(parent::getfullpath());
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif(parent::getfullpath());
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng(parent::getfullpath());
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=80, $permissions=777) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return $this->image_info[0];
}
function getHeight() {
return $this->image_info[1];
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
echo "resized to:\n";
echo $width . "\n";
echo $height . "\n";
echo "imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight())\n";
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
function format(){
$width = $this->getWidth();
$height = $this->getHeight();
if ($width > $height){
return "landscape";
}
if ($height > $width){
return "portrait";
}
if ($height == $width){
return "square";
}
}
function isTooBig($maxsize){
$width = $this->getWidth();
$height = $this->getHeight();
if ($width > $maxsize || $height > $maxsize){
return true;
}
else{
return false;
}
}
function __destruct() {
;
}
}
?>
Now that I got this, I loop through images located in a folder that way:
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
//Instantiate the object
$image = new classimage($directory . "/" . $file);
//And do quite a lot of verification and particularly this:
if ($image->format() == "landscape") {
echo "width: " . $image->getWidth() . "\n";
echo "height: " . $image->getHeight() . "\n";
if ($image->isTooBig($maxsize_mini)) {
$image->resizeToWidth($maxsize_mini);
$image->save($directory . "/mini_" . $image->getfilename());
}
}
}
And now to the problem:
I echo'ed the resize function to see what's being run and I got that:
picture1
imagecopyresampled(Resource id #26, Resource id #25, 0, 0, 0, 0, 608, 405.65, (), ())
picture2
imagecopyresampled(Resource id #27, Resource id #26, 0, 0, 0, 0, 250, 166.796875, (), ())
It looks like it's reusing an instantiation of the previous object and so on and so forth.
In my mind, the way it should look is:
picture1
imagecopyresampled(Resource id #2, Resource id #1, 0, 0, 0, 0, 608, 405.65, (), ())
picture2
imagecopyresampled(Resource id #2, Resource id #1, 0, 0, 0, 0, 250, 166.796875, (), ())
The previous object is destructed, the new one has the old id.
how does that work?
The reason I ask and wonder about this is because I'm having weird result for the resize such as a ridiculously small picture mini size in the actual mini size picture (250 px) except whatever is left is filled with black.