我从这里得到了一个代码 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/