0

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; }   } ?>

Edited by pritaeas: Moved to PHP

3
Contributors
7
Replies
31
Views
2 Years
Discussion Span
Last Post by SimonIoa
1

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.

1

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.

Edited by pixelsoul

0

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>';
                }
0
$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>';
0

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?

1

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.