我正在将产品截图上传到我的网站...我想原样上传原始图片并为其创建缩略图,但在上传这两个文件后,创建的缩略图的文件大小比预期的要大一些。有没有什么方法可以减少缩略图的文件大小而不影响PHP的质量或使用Imagemagick(我不知道如何使用但我愿意学习,如果需要)...
下面是我用来上传文件的代码..
<form action="<?php echo $_server['php-self']; ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
<button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
<?php
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "images/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "images/" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$tn = imagecreatetruecolor($width, $height) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ;
imagejpeg($tn, $save, 100) ;
$save = "images/sml_" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 130;
$diff = $width / $modwidth;
$modheight = 185;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
echo "Large image: <img src='images/".$imagepath."'><br>";
echo "Thumbnail: <img src='images/sml_".$imagepath."'>";
}
} ?>
请指出我正确的方向......谢谢
答案 0 :(得分:16)
不要传递100作为imagejpeg()的质量 - 超过90的任何东西通常都是矫枉过正的,只会让你获得更大的JPEG。对于缩略图,请尝试75并向下工作,直到质量/尺寸权衡可以接受为止。
//try this
imagejpeg($tn, $save, 75) ;
答案 1 :(得分:4)
您好@halocursed我只是尝试使用您的代码压缩不同的图像类型,如png和gif,而不是图像变黑。所以,我修改代码块并为jpg工作,png。
<?php
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
// print_r($_FILES); die;
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "images/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "images/" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file
list($width, $height) = getimagesize($file);
$tn = imagecreatetruecolor($width, $height);
//$image = imagecreatefromjpeg($file);
$info = getimagesize($target);
if ($info['mime'] == 'image/jpeg'){
$image = imagecreatefromjpeg($file);
}elseif ($info['mime'] == 'image/gif'){
$image = imagecreatefromgif($file);
}elseif ($info['mime'] == 'image/png'){
$image = imagecreatefrompng($file);
}
imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($tn, $save, 60);
echo "Large image: ".$imagepath;
}
}
?>
答案 2 :(得分:2)
75是默认的质量设置,但是如果使用它,你会发现质量会大大降低。 90为您提供了出色的图像质量,并将文件大小减少了一半,如果您想减小文件大小,甚至更多地使用85或80,但没有任何相似之处。
答案 3 :(得分:2)
它应该是60,它代表60%。
实施例: 如果你在Photoshop中打开一个图像并尝试将其保存为web并选择jpg,你可以看到它使用60仍然是高质量,但文件较小。如果你想要更低,更多的退化,意味着颜色更加扭曲。
超过60个没有给你更好的东西,只有更大的文件大小。
这是网络的标准图像优化。保持高品质,但保持尽可能低的文件大小。
答案 4 :(得分:1)
100%的质量设置非常大。尝试85或90%你不会在大多数图像上看到差异。
请参阅: http://www.ampsoft.net/webdesign-l/jpeg-compression.html
答案 5 :(得分:1)
如果使用jpeg,使用75到85之间的质量通常是可以接受的(并且100占用太多空间,对于不那么重要的增益,顺便说一下),至少对于照片而言。
作为旁注,如果您正在处理屏幕截图,使用PNG可能会让您获得更好的质量:jpeg会降低图像(这对于照片来说非常好,但对于屏幕截图,使用按像素绘制的字体,它可以带来一些不好看的效果)
答案 6 :(得分:0)
将压缩的图像与原始图像一起移动。实际上,我只想移动以“ rxn-”开头的压缩图像。
include("do.php");
session_start();
if(is_array($_FILES)) {
for($i=0; $i<count($_FILES['userImage']['tmp_name']); $i++){
if(is_uploaded_file($_FILES['userImage']['tmp_name'][$i])) {
$sourcePath = $_FILES['userImage']['tmp_name'][$i];
$upload_dir = "images/";
$targetPath = "images/".basename($_FILES['userImage']['name'][$i]);
$source_image = "images/".basename($_FILES['userImage']['name'][$i]);
$imageName =$_FILES['userImage']['name'][$i];
$imageFileType = strtolower(pathinfo($targetPath,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["userImage"]["tmp_name"][$i]);
if (file_exists($targetPath)) {
// echo "<span style='color:red;'> file already exists</span> ";
}
if($check !== false) {
// echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "<span style='color:red;'>File is not an image.</span><br>";
$uploadOk = 0;
}
if($imageFileType == "jpg" || $imageFileType == "png" || $imageFileType == "jpeg"
|| $imageFileType =="gif" ) {
if(move_uploaded_file($sourcePath,$targetPath))
//if(1)
{
$image_destination = $upload_dir."rxn-".$imageName;
$compress_images = compressImage($source_image, $image_destination);
$pId=$_SESSION['pId'];
$sql="insert into image(p_id,img_path) values('$pId','$compress_images')";
$result = mysql_query($sql);
echo "
<div class='col-sm-3' id='randomdiv' style='padding-bottom: 15px;'>
<div class='bg_prcs uk-height-small uk-flex uk-flex-center uk-flex-middle uk-background-cover uk-light uk-card-default uk-card-hover imgt' data-src='$image_destination' uk-img>
</div>
</div>
";
}
}
else{ echo "<span style='color:red;'> only JPG, JPEG, PNG & GIF files are allowed.</span>";
}
}
}
}
// created compressed JPEG file from source file
function compressImage($source_image, $compress_image) {
$image_info = getimagesize($source_image);
if ($image_info['mime'] == 'image/jpeg') {
$source_image = imagecreatefromjpeg($source_image);
imagejpeg($source_image, $compress_image, 75);
} elseif ($image_info['mime'] == 'image/gif') {
$source_image = imagecreatefromgif($source_image);
imagegif($source_image, $compress_image, 75);
} elseif ($image_info['mime'] == 'image/png') {
$source_image = imagecreatefrompng($source_image);
imagepng($source_image, $compress_image, 6);
}
return $compress_image;
}
?>