在图片上传时创建缩略图 - 'SOME' 是黑色的,但其他的还可以
creating thumbnails on image upload - 'SOME' are black, yet others are ok
我有以下图片上传脚本,它可以上传图片、重命名和调整缩略图的大小。上传图片时(全部为JPG)有的上传显示正常,有的上传主图正常但缩略图只是一个黑色方块。
有人知道问题出在哪里吗?
[edit] 我已将 PNG 添加到文件类型,它可以正确上传,但是 'real' JPG 文件现在上传黑色缩略图。我在想我需要以某种方式检查文件扩展名并根据返回的内容应用 imagecreatefromjpeg/imagecreatefrompng 吗?
如果是这样的话,我在想一个 if/else 突出显示的语句可以吗? - 不确定要检查什么以获得扩展名 (jpg/png) 但是...
if(isset($_POST['submit'])){
// get file info
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$thumbName = $_FILES['file']['name']; //
$thumbTmpName = $_FILES['file']['tmp_name']; //
$thumbSize = $_FILES['file']['size']; //
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
//allow file types
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png'); // ADDED PNG HERE
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
if($fileSize < 1000000){
$fileNameNew = $row['item_img'].".".$fileActualExt; //code!
$fileDestination = 'images/'.$col_id.'/'.$fileNameNew; //number
move_uploaded_file($fileTmpName, $fileDestination);
// if(???){ // START POSSIBLE IF/ELSE HERE?
// create thumbnail
$src = imagecreatefromjpeg($fileDestination);
list($width, $height) = getimagesize($fileDestination);
$thumbWidth = 100;
$thumbHeight = 100;
// } else { // POSSIBLE ELSE HERE?
// ADDED THIS FOR PNG
$src = imagecreatefrompng($fileDestination);
list($width, $height) = getimagesize($fileDestination);
$thumbWidth = 100;
$thumbHeight = 100;
// } // END POSSIBLE IF/ELSE HERE?
$tmp = imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($tmp, $src, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);
imagejpeg($tmp, 'images/'.$col_id.'/thumbs/'.$fileNameNew.'', 100);
imagedestroy($src);
imagedestroy($tmp);
//CHECK FOR IMAGE
$checkImgQuery = $db->prepare("SELECT item_image.item_image, item_item.item_id FROM item_image JOIN item_item ON item_item.item_img = item_image.item_image WHERE item_id = :item_id");
$checkImgQuery->execute(array(':item_id' => $item_id));
$check = $checkImgQuery->rowCount();
if($check > 0){
// UPDATE
} else {
// ADD
}
} else {
$error[] = 'your file is too big';
}
} else {
$error[] = 'there was an error uploading your file';
}
} else {
$error[] = 'you cannot upload files of this type';
}
}
由于它并不总是发生,这几乎可以肯定来自文件,因为它们是唯一发生变化的东西......我猜你的 jpeg 文件要么已损坏(但不足以不显示图片,但可能会使 PHP 代码崩溃)或者它们根本不是真正的 jpeg...
这是我的建议:没有必要强制用户只使用一种文件类型,而 PHP 单独可以处理多种文件类型,因此我们可以检查 mime 类型并采取相应的行动。
switch(mime_content_type($fileDestination){
case 'image/jpeg':
$src = imagecreatefromjpeg($fileDestination);
break;
case 'image/png':
$src = imagecreatefrompng($fileDestination);
break;
case 'image/gif':
$src = imagecreatefromgif($fileDestination);
break;
default:
$error[] = 'your file is not of a recognized type';
}
if(isset($src)){
//do what you were doing.
}
于是我们测试mime类型,使用相应的功能。并不是说我没有包括所有内容,但这应该是一个好的开始。
如果你想要一个更简单的实现,你可以使用这个:
$src = imagecreatefromstring(file_get_content($fileDestination));
从字符串创建图像时,会自动检测类型。如果无法识别文件,这将 return 错误。
从 PNG 保存为 JPG 时,您需要注意透明度...但我会把这部分留给您。
我有以下图片上传脚本,它可以上传图片、重命名和调整缩略图的大小。上传图片时(全部为JPG)有的上传显示正常,有的上传主图正常但缩略图只是一个黑色方块。
有人知道问题出在哪里吗?
[edit] 我已将 PNG 添加到文件类型,它可以正确上传,但是 'real' JPG 文件现在上传黑色缩略图。我在想我需要以某种方式检查文件扩展名并根据返回的内容应用 imagecreatefromjpeg/imagecreatefrompng 吗? 如果是这样的话,我在想一个 if/else 突出显示的语句可以吗? - 不确定要检查什么以获得扩展名 (jpg/png) 但是...
if(isset($_POST['submit'])){
// get file info
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$thumbName = $_FILES['file']['name']; //
$thumbTmpName = $_FILES['file']['tmp_name']; //
$thumbSize = $_FILES['file']['size']; //
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
//allow file types
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png'); // ADDED PNG HERE
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
if($fileSize < 1000000){
$fileNameNew = $row['item_img'].".".$fileActualExt; //code!
$fileDestination = 'images/'.$col_id.'/'.$fileNameNew; //number
move_uploaded_file($fileTmpName, $fileDestination);
// if(???){ // START POSSIBLE IF/ELSE HERE?
// create thumbnail
$src = imagecreatefromjpeg($fileDestination);
list($width, $height) = getimagesize($fileDestination);
$thumbWidth = 100;
$thumbHeight = 100;
// } else { // POSSIBLE ELSE HERE?
// ADDED THIS FOR PNG
$src = imagecreatefrompng($fileDestination);
list($width, $height) = getimagesize($fileDestination);
$thumbWidth = 100;
$thumbHeight = 100;
// } // END POSSIBLE IF/ELSE HERE?
$tmp = imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($tmp, $src, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);
imagejpeg($tmp, 'images/'.$col_id.'/thumbs/'.$fileNameNew.'', 100);
imagedestroy($src);
imagedestroy($tmp);
//CHECK FOR IMAGE
$checkImgQuery = $db->prepare("SELECT item_image.item_image, item_item.item_id FROM item_image JOIN item_item ON item_item.item_img = item_image.item_image WHERE item_id = :item_id");
$checkImgQuery->execute(array(':item_id' => $item_id));
$check = $checkImgQuery->rowCount();
if($check > 0){
// UPDATE
} else {
// ADD
}
} else {
$error[] = 'your file is too big';
}
} else {
$error[] = 'there was an error uploading your file';
}
} else {
$error[] = 'you cannot upload files of this type';
}
}
由于它并不总是发生,这几乎可以肯定来自文件,因为它们是唯一发生变化的东西......我猜你的 jpeg 文件要么已损坏(但不足以不显示图片,但可能会使 PHP 代码崩溃)或者它们根本不是真正的 jpeg...
这是我的建议:没有必要强制用户只使用一种文件类型,而 PHP 单独可以处理多种文件类型,因此我们可以检查 mime 类型并采取相应的行动。
switch(mime_content_type($fileDestination){
case 'image/jpeg':
$src = imagecreatefromjpeg($fileDestination);
break;
case 'image/png':
$src = imagecreatefrompng($fileDestination);
break;
case 'image/gif':
$src = imagecreatefromgif($fileDestination);
break;
default:
$error[] = 'your file is not of a recognized type';
}
if(isset($src)){
//do what you were doing.
}
于是我们测试mime类型,使用相应的功能。并不是说我没有包括所有内容,但这应该是一个好的开始。
如果你想要一个更简单的实现,你可以使用这个:
$src = imagecreatefromstring(file_get_content($fileDestination));
从字符串创建图像时,会自动检测类型。如果无法识别文件,这将 return 错误。
从 PNG 保存为 JPG 时,您需要注意透明度...但我会把这部分留给您。