【发布时间】:2013-05-09 15:57:43
【问题描述】:
我的问题类似于this one,但我被困在最后一点!
以下代码获取 HTML 表单发送的文件,并根据两个预定义值 $maxwidth 和 $maxheight 检查其尺寸。如果它太宽,它会调整图像的大小。
我接下来要做的就是将新调整大小的图像重新分配给原始变量$tmpName,以便脚本的其余部分可以处理它。
以下代码返回这些错误:
Warning: fopen() expects parameter 1 to be string, resource given in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 43
Warning: filesize() [function.filesize]: stat failed for Resource id #4 in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 44
Warning: fread(): supplied argument is not a valid stream resource in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 44
Warning: fclose(): supplied argument is not a valid stream resource in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 46
我想我已经很接近了,但有人可以帮忙吗?谢谢。
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
list($width,$height)=getimagesize($tmpName);
if ($width>$height && $width>$maxwidth) {
$newheight=($height/$width)*$maxwidth;
$newwidth=$maxwidth;
$imageResized = imagecreatetruecolor($newwidth, $newheight);
$imageTmp = imagecreatefromjpeg ($tmpName);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$tmpName=$imageResized;
// My problem lies somewhere here ^^^^
}
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
// Create the query and insert
// into our database.
$query = "INSERT INTO tblPrints ";
$query .= "(title,full_image) VALUES ('IMG-TEST','$data')";
$results = mysql_query($query, $link);
// Print results
print "Thank you, your file has been uploaded.";
}
else {
print "No image selected/uploaded";
}
【问题讨论】:
-
而不是将其分配给 $imgTmp 变量,而是将其分配给 $tmpName 简单
-
你到底是什么意思,亚西尔?
-
在处理你分配给一个新的变量 $imageTmp 只需继续 $tmpName 这样你的变量的名称就不会改变,你可以用相同的名称处理它。
-
在使用 fopen 打开文件后尝试转义(使用 mysql_real_escape())$tmpName。
-
$tmpName 包含名称为字符串,但 imagecreatetruecolor 返回图像对象的资源句柄。两种完全不同的类型。只需在重新采样到新文件后再次保存调整大小的图像,并将其路径用作 $tmpName 中的字符串。
标签: php mysql database image-resizing image-upload