一起上传图像和缩略图,但未生成缩略图
Uploading image and thumbnail together but the thumbnail is not generating
我正在尝试向我的现有代码中添加一种方法,以从正在上传的全尺寸图像生成缩略图。我在这里做了一些搜索,找到了一个函数来完成我正在寻找的东西,但没有生成我的缩略图。我设置它的方式是将全尺寸图像转到 images/lg/
文件夹,缩略图将转到 images
文件夹。但是,只有大图像会进入各自的文件夹,而不会生成缩略图。
<?php
include_once 'includes/dbh.inc.php';
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
if (isset($_GET['id'])){
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed))
if($fileError === 0){
if ($fileSize < 5000000) {
$fileNameNew = "car".$_GET['id'].".".$fileActualExt;
$fileDestination = 'images/lg/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination); //files going to the 'images/lg' folder
$sql = "UPDATE rollingstock SET stockImgStatus=? WHERE stockID=?";
$stmt = $mysqli->prepare($sql);
$stockImgStatus = 1;
if (file_exists($fileDestination)) { /*if the file exisits make a thumbnail */
$src = $fileDestination;
$dest = 'images/'.$fileNameNew;
$desired_width = "395";
make_thumb($src, $dest, $desired_width); //Thumbnails going to the 'imgages' folder
}
if ($stmt &&
$stmt -> bind_param('ii', $stockImgStatus, $_GET['id'])&&
$stmt -> execute()&&
$stmt -> affected_rows === 1
) {
header("Location: list.php?list=all");
}else{
echo "Not Updated";
}
}else {
echo "Your file is too big!";
}
}else{
echo "There was an error uploading for file!";
}
}else{
echo "You cannot upload files of this type!";
}
}
我可以使用提供的代码生成大图像和缩略图。
请检查您的图片文件夹是否有写入权限。
我检查了你的代码,总体来说没问题。 make_thumb()
函数中只有一个小错误,您在其中使用了 imagecreatefromjpeg()
,它适用于 .jpeg 和 .jpg 图像类型,但不适用于您也接受的 .png 图像。
您可以简单地在 make_thumb()
函数中添加一个检查来检查文件类型并调用适当的方法,就像提到的 in this post.
我还从链接 post:
添加了一个修改过的片段
switch ( strtolower( pathinfo( $src, PATHINFO_EXTENSION ))) {
case 'jpeg':
case 'jpg':
$source_image = imagecreatefromjpeg($src);
break;
case 'png':
$source_image = imagecreatefrompng($src);
break;
case 'gif':
$source_image = imagecreatefromgif($src);
break;
default:
throw new InvalidArgumentException('File "'.$src.'" is not valid jpg, png image.');
break;
}
我正在尝试向我的现有代码中添加一种方法,以从正在上传的全尺寸图像生成缩略图。我在这里做了一些搜索,找到了一个函数来完成我正在寻找的东西,但没有生成我的缩略图。我设置它的方式是将全尺寸图像转到 images/lg/
文件夹,缩略图将转到 images
文件夹。但是,只有大图像会进入各自的文件夹,而不会生成缩略图。
<?php
include_once 'includes/dbh.inc.php';
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
if (isset($_GET['id'])){
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed))
if($fileError === 0){
if ($fileSize < 5000000) {
$fileNameNew = "car".$_GET['id'].".".$fileActualExt;
$fileDestination = 'images/lg/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination); //files going to the 'images/lg' folder
$sql = "UPDATE rollingstock SET stockImgStatus=? WHERE stockID=?";
$stmt = $mysqli->prepare($sql);
$stockImgStatus = 1;
if (file_exists($fileDestination)) { /*if the file exisits make a thumbnail */
$src = $fileDestination;
$dest = 'images/'.$fileNameNew;
$desired_width = "395";
make_thumb($src, $dest, $desired_width); //Thumbnails going to the 'imgages' folder
}
if ($stmt &&
$stmt -> bind_param('ii', $stockImgStatus, $_GET['id'])&&
$stmt -> execute()&&
$stmt -> affected_rows === 1
) {
header("Location: list.php?list=all");
}else{
echo "Not Updated";
}
}else {
echo "Your file is too big!";
}
}else{
echo "There was an error uploading for file!";
}
}else{
echo "You cannot upload files of this type!";
}
}
我可以使用提供的代码生成大图像和缩略图。 请检查您的图片文件夹是否有写入权限。
我检查了你的代码,总体来说没问题。 make_thumb()
函数中只有一个小错误,您在其中使用了 imagecreatefromjpeg()
,它适用于 .jpeg 和 .jpg 图像类型,但不适用于您也接受的 .png 图像。
您可以简单地在 make_thumb()
函数中添加一个检查来检查文件类型并调用适当的方法,就像提到的 in this post.
我还从链接 post:
添加了一个修改过的片段switch ( strtolower( pathinfo( $src, PATHINFO_EXTENSION ))) {
case 'jpeg':
case 'jpg':
$source_image = imagecreatefromjpeg($src);
break;
case 'png':
$source_image = imagecreatefrompng($src);
break;
case 'gif':
$source_image = imagecreatefromgif($src);
break;
default:
throw new InvalidArgumentException('File "'.$src.'" is not valid jpg, png image.');
break;
}