PHP:getimagesize() 在 LAMP 服务器上失败
我正在使用这个函数:
function image_resizer($max_width,$max_height,$img_path) {
list($width, $height) = getimagesize($img_path);
if (($width > $max_width) or ($height > $max_height)) {
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
$width = intval($ratio*$width);
$height = intval($ratio*$height);
}
return 'width="' . $width . '" height="' . $height . '"';
}
...用此代码调用(定义位于粘贴在此处的另一个文件中以供说明):
define("SITE_URI", "http://dev.projectname.co.uk/");
define("PRODUCT_IMAGES_URI", "images/collection/");
<?php echo image_resizer(280, 375, SITE_URI . PRODUCT_IMAGES_URI . $display_image); ?> alt="<?php echo $display_image; ?>
...其中 $display_image 来自数据库(成功)。并收到以下错误:
警告:getimagesize(http://dev.projectname.co.uk/images/collection/filename.jpg)[function.getimagesize]:无法打开流:HTTP请求失败! /var/www/projectname.co.uk/dev/admin/includes/functions_admin.php 第 59 行需要 HTTP/1.1 401 授权 width="" height="" alt="文件名.jpg"/>
我正在使用 getimagesize() 从我最初向服务器上的 www-data 用户授予权限的文件夹中获取图像的大小,然后当我再次收到错误时,我只是 chmod 777 到图像文件夹。我现在很茫然。
I'm using this function:
function image_resizer($max_width,$max_height,$img_path) {
list($width, $height) = getimagesize($img_path);
if (($width > $max_width) or ($height > $max_height)) {
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
$width = intval($ratio*$width);
$height = intval($ratio*$height);
}
return 'width="' . $width . '" height="' . $height . '"';
}
...called with this code (the defines are in another file pasted here for illustration):
define("SITE_URI", "http://dev.projectname.co.uk/");
define("PRODUCT_IMAGES_URI", "images/collection/");
<?php echo image_resizer(280, 375, SITE_URI . PRODUCT_IMAGES_URI . $display_image); ?> alt="<?php echo $display_image; ?>
...where $display_image is coming from the DB (successfully). And getting the following error:
Warning: getimagesize(http://dev.projectname.co.uk/images/collection/filename.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /var/www/projectname.co.uk/dev/admin/includes/functions_admin.php on line 59
width="" height="" alt="filename.jpg" />
I'm using getimagesize() to get the size of an image from a folder that I initially gave rights to the www-data user on the server, then when I got the error again I just chmod 777 to the image folder. I'm now at a loss.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案很简单,我试图使用 url 来访问服务器上的文件夹,我需要服务器上文件夹的绝对路径。这在本地计算机上运行并通过裂缝溜到了开发服务器。
The answer was simple, I was attempting to use a url to access a folder on the server, I needed the absolute path to the folder on the server instead. This had worked on the local machine and slipped through the cracks to the development server.
问题不在于
getimagesize()
,问题在于您尝试从受密码保护的 URL 检索它。将用户名和密码作为 URL 的一部分传递,或者通过其他方式获取。The problem isn't
getimagesize()
, the problem is that you're trying to retrieve it from a password-protected URL. Either pass the username and password as part of the URL, or get it some other way.