3

我从这里得到了一个代码http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php来让用户发送的图像被调整大小。

这是它如何处理发送的图像。

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

这是其中的一部分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;
}      

我不会粘贴所有代码,因为直到这里一切都工作正常。

问题是我在直接从手机相机上传照片时遇到了方向问题,所以我写了这个:

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

但是当我运行它时,服务器说:

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

这是第 101 行:$exif = exif_read_data($this->image, 0, true);

我已经搜索了有关的问题exif_read_data(),但我找不到“给定资源”的含义,正如我在其他问题和文档中看到的那样,您可以使用临时图像作为参数。

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

4

1 回答 1

2

$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);
   // ...
}
于 2013-03-04T00:11:17.427 回答