上传图片无效后
上传图片时出现问题。它如下:上传图片无效后
我上传了一张图片,并寻找类型(允许使用jpeg,jpg,gif和png)。现在我剪掉一部分,并将其保存在由类型信息创建的临时资源上(如果类型是jpg或jpeg,我使用imagejpeg(),使用PNG我使用imagepng()和gif I使用imagegif())。现在这个工作。然后我再次保存图像。
然后我通过imagecreatefromjpeg/-png/-gif重新打开它们。然后我得到的错误
Warning: imagecreatefromgif() [function.imagecreatefromgif]: 'uploads/gif/test.gif' is not a valid GIF file in /home/blabla/sliceit.php on line 88
线88个看起来如下:
$org_img = 'uploads/' . $name . "/" . $rand . (substr($type,0,1) != "." ? "." . $type : $type);
...
87: elseif ($type == ".gif") {
88: $src_img = imagecreatefromgif($org_img);
89: }
同样的错误也发生与PNG,但不能与JPEG(因为我写了下面的语句开头:
ini_set('gd.jpeg_ignore_warning', 1);
)。 Jpeg警告似乎已停用,但不是png和gif的警告。我用mspaint创建了图像,所以它们实际上必须是有效的。
感谢您的帮助。
弗洛
编辑:一些代码:
$name = 'something';
$filetype = substr($_FILES['datei']['name'],-4,4);
$filetype = strtolower($filetype);
$randomsessid = randomstring(60);
mkdir('uploads/' . $name);
move_uploaded_file($_FILES['datei']['tmp_name'],'uploads/' . $name . '/' . $randomsessid . (substr($filetype,0,1) == "." ? $filetype : "." . $filetype));
mysql_query("INSERT INTO SESSIONS VALUES('','" . $name . "','" . $randomsessid . "','" . strtolower($filetype) . "'," . time() . ")");
所以现在我保存的文件,在我的表中的信息。
现在我链接到另一个文件...
$id = mysql_real_escape_string($_GET["randid"]); //here I get the randomstring
if ($id == "") {
exit;
}
$unf = mysql_query("SELECT NAME, TYP FROM SESSIONS WHERE RANDOM = '" . $id . "'");
if (mysql_num_rows($unf) == 1) {
$f = mysql_fetch_object($unf);
$name = $f->NAME;
$filetype = $f->TYP;
}
else {
exit;
}
$image_resize = new image_resize; //this is a very useful class to resize images
$size = $_GET["size"]; //here is 'auto' inside
$log->debug('size: ' . $size);
if ($size == "custom" and isset($_GET["x"]) and isset($_GET["y"])) {
//blabla some code...
}
else {
$image_resize->load("uploads/" . $name . "/" . $id . (substr($filetype,0,1) == "." ? $filetype : "." . $filetype));
$image_resize->resize(600,600);
$image_resize->save("uploads/" . $name . "/" . $id . (substr($filetype,0,1) == "." ? $filetype : "." . $filetype));
}
而现在另一重定向....
ini_set('gd.jpeg_ignore_warning', 1);
$id = $_GET["randid"];
if ($id == "") {
exit;
}
$tempsel = "SELECT * FROM SESSIONS WHERE RANDOM = '" . $id . "'";
$unf = mysql_query($tempsel);
if (mysql_num_rows($unf) != 1) {
$log->debug('tempsel: ' . $tempsel);
exit;
}
$f = mysql_fetch_object($unf);
$name = $f->NAME;
$type = $f->TYP;
for ($i = 1; $i <= 9; $i++) {
createImagePart($i,$name,$type,$id,$log); //$i = for loop, $name = the name from the beginning, $type defined, $id = random id, $log = a previously defined log class.
}
和被叫功能createImagePartI():
function createImagePart($nr,$name,$type,$id,$log) {
if (!isFolderSet($id . "/parts/")) {
mkdir("uploads/" . $id);
mkdir("uploads/" . $id . "/parts");
}
//prepare params....
$org_img = 'uploads/' . $name . "/" . $id . (substr($type,0,1) != "." ? "." . $type : $type);
$dst_img = 'uploads/' . $id . "/parts/" . $nr . (substr($type,0,1) != "." ? "." . $type : $type);
$tmp_img = imagecreatetruecolor(200, 200);
if ($type == ".jpg" or $type == "jpeg") {
$src_img = imagecreatefromjpeg($org_img);
}
elseif ($type == ".png") {
$src_img = imagecreatefrompng($org_img);
}
elseif ($type == ".gif") {
$src_img = imagecreatefromgif($org_img);
}
else {
exit;
}
$sX = ($nr-1)%3 * 200; //// watch this question:
$sY = floor(($nr-1)/3) * 200; //// http://*.com/questions/6325169/variable-has-unexpected-value
imagecopy($tmp_img, $src_img, 0,0, $sX, $sY, 200, 200);
if ($type == ".jpg" or $type == "jpeg") {
imagejpeg($tmp_img, $dst_img,100); // because of ini_set i dont get an error here
}
elseif ($type == ".png") {
imagepng($tmp_img, $dst_img, 0); //on these functions, I get the errors
}
else {
imagegif($tmp_img, $dst_img); //also here i get an error
}
imagedestroy($tmp_img);
}
有没有太多的工作,但有时当你使用像你这样的过程时,PHP会将图像标题更改为JFIF(JPEG)广告的GIF98a(GIF)所以运行检查头之前,您使用imagecreatefrom .... 希望这有助于一点点,也许更多的代码会帮助我们所有?
我认为它不会发布我的所有代码,但我会尝试发布所有在此配置中执行的代码。 – 2011-06-14 21:09:26
这更多的是我的意思,多一点与正在发生的事情有关 – 2011-06-14 21:11:59
刚发布的所有相关的方式 – 2011-06-14 21:24:58
你确定该图像存在? – 2011-06-14 21:02:53
'$ org_img'是否真的指向现有的图像文件? (也许有一个错字或什么?);此外,最好使用['getimagesize()'](http://php.net/manual/en/function.getimagesize.php)进行类型检测,因为它有助于区分大小写并检查实际内容比文件扩展名。 – mkilmanas 2011-06-14 21:03:27
我会建议用一种图像做一个清晰的例子。在你的代码示例中,$ type对我来说看起来有些困惑。 – powtac 2011-06-14 21:04:02