怎么能没有功能压缩图像?

时间:2016-02-04 15:16:38

标签: php ajax image function

我上传前总是使用http://compressjpeg.com/压缩图片,这不太实用。我想用php创建一个像http://compressjpeg.com/这样的压缩器。

我有这个代码

<?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;
        }
?>

有没有办法在不使用函数的情况下进行压缩?也许是代码太多了。 以及如何创建两个不同大小的图像?例如图像A=900px x 600pxB=120px x 500px?感谢

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;
        }
?>

1 个答案:

答案 0 :(得分:0)

我认为您需要安装<img class="mascot img-responsive" src="http://farm5.static.flickr.com/4017/4717107886_dcc1270a65_b.jpg"> 扩展程序并使用它:
- detect image type and create image
- 然后通过imagecopyresized()

调整大小

我想建议通过两个步骤调整大小,例如:
- 我们有一张图片1920x1080
- 我们需要制作缩略图160x90
- 通过imagecopyresized()调整大小为640x360的临时图像 - 通过imagecopyresampled()将之前的结果调整为目标尺寸160x90左右

这两步变换的目的是目标图像的质量,它比仅使用图像复制更好,并且比仅使用imagecopyresampled更快