【发布时间】:2013-01-08 21:46:40
【问题描述】:
我正在尝试制作缩略图。并且 php 函数有效。我已经用它制作了缩略图。但是,某些文件不起作用。我已经缩小了问题的范围,但现在卡住了。任何帮助将不胜感激。
图片可以在http://www.pspsigma.com/image_test.php 找到。左边那个不行。右边那个会。两者都是.jpg。两者最初都是通过表单上传的,但已针对堆栈溢出进行了更改,因此该函数可以在没有表单的情况下工作。
html 是一个简单的脚本,带有文件输入设置操作到这个 .php 文件
php:
<?php
$the_name= $_FILES['userfile']['name'];
$tmpname = $_FILES['userfile']['tmp_name'];
$size="1000";
$save_dir="images/thumbs/";
$save_name=$the_name;
$maxisheight= "100";
function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight)
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = getimagesize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
//check if imorig is set
if (!isset($imorig)) {
echo " imorig is not set, ";
}
//I tried setting $x and $y from the width and height of the original file. Still didn't work.
//although the variables were set.
//this makes me think the problem is with the imagecreatefromjpeg function.
list($width, $height, $type, $attr) = getimagesize($tmpname);
echo "getimagesize width= " . $width . ", ";
echo "getimagesize height= " . $height . ", ";
//possible solution, didn't work
//$x = $width;
//$y = $height;
//This is the problem
$x = imagesx($imorig);
$y = imagesy($imorig);
//check that x and y are set
if (!isset($x) && !isset($y)) {
echo" x and y are not set,";
}
elseif (!isset($x)) {
echo" x is not set";
}
elseif (!isset($y)) {
echo" y is not set";
}
else {
echo "|x= " . $x . " |";
echo "|y= " . $y . " |";
}
$woh = (!$maxisheight)? $gis[0] : $gis[1] ;
if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight){
$aw = $size;
$ah = $size * $y / $x;
} else {
$aw = $size * $x / $y;
$ah = $size;
}
}
$im = imagecreatetruecolor($aw,$ah);
if (!$im) {
echo " failure: no im variable. ";
}
else {
if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y)) {
if (!imagejpeg($im, $save_dir.$save_name)) {
echo "failure";}
else {
echo "success";}
}
else {
echo" didn't copy resampled";
}
}
}
img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight);
?>
如果我选择 rush vader.jpg 文件并通过这个函数。它回显了 x 和 y 已设置,显示它们的值和成功。
如果我选择了冷静.jpg,它将回显“失败:没有 im 变量”。并且 $x 和 $y 变量将没有值。如果我为 x 和 y 变量分配 getimagesize($tmp_name) 中的值,它会回显“没有复制重新采样”。
这是实际图像的问题吗?或者有什么方法可以更改代码以便每个图像都能正常工作?
谢谢!!!
更新
我回显了每个变量以查看是否有问题。问题似乎出在类型上。类型输出为 6,我从 php 手册 cmets 中发现是 bmp。
我不明白如果扩展名是 .jpg... 如果是这样的话。是否可以更改类型以使其成为 .jpg、.png 或 .gif?
【问题讨论】:
-
你能不能选择
calm.jpg和imagecreatetruecolor之前的var_dump($aw); var_dump($ah);。这样我们就可以理解发生了什么。 -
该功能甚至没有达到那么远。它在 $x 和 $y 设置的地方失败。虽然, var_dump 会提供一些好处吗?它有什么作用?我不明白你的意思我可以选择calm.jpg吗?
-
如果你阅读
imagecreatetruecolor的PHP页面,失败时返回false。此函数因参数或 GD 库而失败。由于您说它适用于某些图像,这意味着 GD 库没有问题。因此,我看到的唯一问题是这个函数参数没有合适的值。通过使用var_dump,我们可以看到这些参数的值并了解实际问题的原因。你说“如果我选择冷静.jpg,它会回显“失败:没有 im 变量。””。因此,我的意思是用这些代码再次选择它 -
我只是这样做了,值返回 NULL 和 NULL。脚本的完整回显是“getimagesize width= 400, getimagesize height= 563, x 和 y 未设置,NULL NULL 失败:没有 im 变量。”
标签: php gd thumbnails