php - exif_read_data() 表示资源给定

标签 php exif

我从这里得到了一个代码 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() 的问题,但我找不到“给定的资源”是什么意思,正如我在其他问题和文档中看到的那样,您可以使用一个临时的图片作为参数。

所以问题是:我如何处理 $image->this 使其不被视为资源?

最佳答案

$this->image 是一个图像资源,由 imagecreatetruecolor() 等函数创建,它是图像的表示。对于 exif 函数,您必须指定(字符串)文件名。

所以它应该是这样的:

function load($filename) {

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

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

关于php - exif_read_data() 表示资源给定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15192432/

相关文章:

php - Preg_replace 价格字符串 (PHP)

javascript - Jquery ajax() 函数不会加载 PHP 脚本

java - 如何使用 Phonegap Android 截图插件旋转截图图像

php - 如何使用 php 自动旋转带有 exif 方向数据的图像

.net - .Net 最好的 EXIF 库是什么?

php - mySQL更新用户信息保存null

php - 我尝试连接到 xampp 数据库但失败

php - MySQL GROUP_CONCAT : Formatting the output

delphi - 将 EXIF 曝光时间计算为分数 (Delphi)

objective-c - 在 OS X 上打开 NSImage 时从 EXIF 读取相机数据