Hello can anyone tell if i am doing this right?

I use this code to resize image with php

include('simpleimage.php');
$query1=mysql_query("select image_path from user_uploads where id=".$p );
                while($row=mysql_fetch_array($query1))
                {
                    $newdata=$row['image_path'];
                    $final_image=$base_url.$path.$newdata;
                    $final_image = new SimpleImage(); 
                    $final_image->load($final_image); 
                    $final_image->resize(50,50); 
                    echo '<a class="show-all-audio" href="#show-all-audio' . $messageid . '" style="cursor:pointer" id="show_all_audio"><img src="'.$final_image.'" class="small_icon" original-title="'.$message.'" ></a>';
                }

The variable $final image worked -showed the image i mean- until i added $final_image = new SimpleImage();

this is the simpleimage.php

class SimpleImage {   var $image; var $image_type;   function load($filename) {   $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) {   $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) {   $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) {   $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {   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 imagesx($this->image); } function getHeight() {   return imagesy($this->image); } 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); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; }   } ?>

Hi, I suppose SimpleImage is this:

Or a similar version of the above, the error in your code is that you're overwriting the variable $final_image with the new instance of SimpleImage:

$final_image = '/path/to/file.jpg';
$final_image = new SimpleImage(); # <- value overwrite
$final_image->load($final_image); # <- error

So change the above to:

$image = '/path/to/file.jpg';
$final_image = new SimpleImage();
$final_image->load($image);

and it should work fine.

Also, echoing out $final_image will not display the image, this is the object. You'll need to save the image after resizing $final_image->save('path/image.jpg'); and then echo out that image to display it.

ok so i did this...
$path is the location i put the photos
$newdata is the file (183748.jpg for example)
what i am missing now?

$query1=mysql_query("select image_path from user_uploads where id=".$p );
                while($row=mysql_fetch_array($query1))
                {
                    $newdata=$row['image_path'];
                    $image=$base_url.$path.$newdata;
                    $final_image = new SimpleImage();
                    $final_image->save($path.$newdata);
                    $final_image->load($image);
                    $final_image->resize(50,50); 
                    echo '<a class="show-all-audio" href="#show-all-audio' . $messageid . '" style="cursor:pointer" id="show_all_audio"><img src="'.$final_image.'" class="small_icon" original-title="'.$message.'" ></a>';
                }
$newdata=$row['image_path'];
$image=$base_url.$path.$newdata;
$final_image = new SimpleImage();
$final_image->load($image);
$final_image->resize(50,50);
$final_image->save($path.$newdata); // need to save image after resizing

// need to echo out the image that was saved, not the object
echo '<a class="show-all-audio" href="#show-all-audio' . $messageid . '" style="cursor:pointer" id="show_all_audio"><img src="'.$path.$newdata.'" class="small_icon" original-title="'.$message.'" ></a>';

yes cereal thats it! Do i need to put an output? And a silly question, just to fully understand this. If i have already saved the file to $path do i need to save it again? So there will be two images? Thats bad for server space right?

You can do $final_image->save($image); which just over writes the original image with the resized one.

ok now its shows but i have two last questions...

  1. When i download the image it still has the original size.
    This is from the properties of the image
    565X565 pixels

  2. and the while function doesnt work it returns only one image when it should return 6

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.