[英]How can compress images without function?
I always compress images with http://compressjpeg.com/ before I upload them, this is not very practical. 在上传图片之前,我总是使用http://compressjpeg.com/压缩图片,这不是很实用。 I want to make a compressor like http://compressjpeg.com/ with php.
我想用php制作类似http://compressjpeg.com/的压缩器。
i have this code 我有这个代码
<?php
require '../../db/sessions.php';
require '../../db/ds.php';
$path = "../../image/color/aqua/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" )
{
$name = $_FILES['aqua-example']['name'];
$size = $_FILES['aqua-example']['size'];
$code = $_POST['aqua-code'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$ext = "jpg";
$actual_image_name = $code.".".$ext;
$tmp = $_FILES['aqua-example']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$query = "UPDATE waiting_item SET aqua='$actual_image_name' WHERE id='$code'";
$res = $mysqli->query($query);
echo "<img src='/image/color/aqua/".$actual_image_name."' width='400' height='400' class='ua6m u766 ".$actual_image_name."'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
?>
Is there a way to compress without using a function? 有没有不使用函数的压缩方法? Maybe it was too much code.
也许是太多的代码。 And how to create two images with different sizes?
以及如何创建两个大小不同的图像? eg image
A=900px x 600px
and B=120px x 500px
? 例如图像
A=900px x 600px
和B=120px x 500px
? Thanks 谢谢
EDITED 已编辑
<?php
require '../../db/sessions.php';
require '../../db/ds.php';
$path = "../../image/color/aqua/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" )
{
$name = $_FILES['aqua-example']['name'];
$size = $_FILES['aqua-example']['size'];
$code = $_POST['aqua-code'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$ext = "jpg";
$actual_image_name = $code.".".$ext;
$tmp = $_FILES['aqua-example']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
// --------------------------------- THIS ----------------------------- //
function Img_Resize($paths) {
$x = getimagesize($paths);
$width = $x['0'];
$height = $x['1'];
$rs_width = $width / 2;
$rs_height = $height / 2;
switch ($x['mime']) {
case "image/gif":
$img = imagecreatefromgif($paths);
break;
case "image/jpeg":
$img = imagecreatefromjpeg($paths);
break;
case "image/png":
$img = imagecreatefrompng($paths);
break;
}
$img_base = imagecreatetruecolor($rs_width, $rs_height);
imagecopyresized($img_base, $img, 0, 0, 0, 0, $rs_width, $rs_height, $width, $height);
$path_info = pathinfo($paths);
switch ($path_info['extension']) {
case "gif":
imagegif($img_base, $paths);
break;
case "jpeg":
imagejpeg($img_base, $paths);
break;
case "png":
imagepng($img_base, $paths);
break;
}
}
$img = Img_Resize($path,$actual_image_name);
// ------------------ end ----------------------------- //
$query = "UPDATE waiting_item SET aqua='$actual_image_name' WHERE id='$code'";
$res = $mysqli->query($query);
echo "<img src='/image/color/aqua/".$actual_image_name."' width='400' height='400' class='ua6m u766 ".$actual_image_name."'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
?>
I think you need to install php5-gd
extension and use it like that: 我认为您需要安装
php5-gd
扩展名并像这样使用它:
- detect image type and create image - 检测图像类型并创建图像
- then resize via imagecopyresized() -然后通过imagecopyresize()调整大小
I would like to recommend resize with two steps, for example: 我建议通过两个步骤来调整大小,例如:
- we have an image 1920x1080 -我们有一张1920x1080的图片
- we need to make a thumbnail 160x90 -我们需要将缩略图制作为160x90
- resize via imagecopyresized() to temporary image with size 640x360 -通过imagecopyresize()调整大小为640x360的临时图像
- resize previous result via imagecopyresampled() to target size 160x90 -通过imagecopyresampled()将先前的结果调整为目标尺寸160x90
the purpose for this two-steps tranforms is quality of target image, it will be much better, than use only imagecopyresized, and faster than use only imagecopyresampled 此两步转换的目的是目标图像的质量,它将比仅使用imagecopyresize更好,并且比仅使用imagecopyresampled更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.