fksr86
fksr86

Reputation: 229

exif_read_data() says resource given

i've got a code from here http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php to get images sent by users resized.

here's how it handles with the image sent.

            include('image_resize.php');
            $image = new SimpleImage();
            $image->load($upload_dir.$filename);
            $image->resizeToWidth(190);
            $image->save($upload_dir.$filename);

here's part of the image_resize.php:

function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $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;
}      

i'm not gonna paste all the code, cause till here eveything is working fine.

the thing is that i'm having an orientation problem when uploading photos directly from mobiles cameras, so i wrote this:

function resize($width,$height) {
    $exif = exif_read_data($this->image, 0, true);
    if(!empty($exif['IFD0']['Orientation'])) {
        switch($exif['Orientation']) {
            case 3: // 180 rotate left
            $this->image = imagerotate($this->image, 180, 0);
            break;

            case 6: // 90 rotate right
            $this->image = imagerotate($this->image, -90, 0);
            break;

            case 8: // 90 rotate left
            $this->image = imagerotate($this->image, 90, 0);
            break;
        }
    }
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}      

but when i run it, the server says:

exif_read_data() expects parameter 1 to be string, resource given in /home/…/public_html/image_resize.php on line 101

this is line 101: $exif = exif_read_data($this->image, 0, true);

i've searched for problems with exif_read_data(), but i couldn't find what "resource given" means, as i've seen in other questions and documentation that you can use a temporary image as a parameter.

so the question is: how can i handle the $image->this to make it not be seen as a resource?

Upvotes: 3

Views: 1292

Answers (1)

Corneliu
Corneliu

Reputation: 1110

$this->image is an image resource, created by functions like imagecreatetruecolor(), which is a representation of an image. For your exif function, you have to specify the (string) filename.

So it should look like:

function load($filename) {

  $this->filename = $filename;
  // ...
}

function resize($width,$height) {
   $exif = exif_read_data($this->filename, 0, true);
   // ...
}

Upvotes: 2

Related Questions