hal*_*sed 12 php image-processing
我正在将产品截图上传到我的网站...我想原样上传原始图像并为其创建缩略图,但在上传两个文件后,创建的缩略图的文件大小比预期的要大.有没有办法我可以减少缩略图的文件大小而不影响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>
Run Code Online (Sandbox Code Playgroud)
<?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."'>";
}
} ?>
Run Code Online (Sandbox Code Playgroud)
请指出我正确的方向......谢谢
Pau*_*xon 16
不要传递100作为imagejpeg()的质量- 超过90的任何东西通常都是矫枉过正的,只会让你获得更大的JPEG.对于缩略图,请尝试75并向下工作,直到质量/尺寸权衡可以接受为止.
//try this
imagejpeg($tn, $save, 75) ;
Run Code Online (Sandbox Code Playgroud)
你好@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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)