调整上传php mysql的宽度并压缩图像

时间:2013-07-15 06:01:28

标签: php file-upload resize compression

我有一个客户端通过iPhone向我发送短信,上面有图片供我上传到他的画廊。我正在尝试创建一个管理系统,以便我可以简单地从文本中获取图像,转到我的iPhone上的管理页面并将图像直接上传到图库。

这可以节省我日常工作时间的大量时间。

使用提供的代码。如何添加以下功能:

  1. 我想尽可能将文件大小压缩到更小的尺寸,类似于Photoshop中的保存到网页jpg功能。 (我得到的大多数图像大约是1-3 MB。我想将它们降低到大约150-500kb)

  2. 我想自动将宽度更改为760px,但要保持宽高比,以免图像被压扁。他向我发送了风景和肖像图片。

  3. 他们是iPhone的形象。他们有一个扩展.JPG(所有大写)我希望这个改为.jpg(全部小写。)这不是一个交易破坏者我只想知道如何做以备将来使用。

  4. 这些功能中的任何一个都非常有用,但所有3个功能都适合我的情况。

    以下是我正在使用的代码?

    这是上传和调整@tman提供的图像的最终正确代码 确保在php.ini文件中安装了imagick。请咨询您的托管服务提供商以进行安装。

    <?php
    include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
    
    
    
    for($i=0;$i<count($_FILES["image"]["name"]);$i++){
    if($_FILES["image"]["name"][$i] != ''){ // don't insert if file name empty
    $dataType = mysql_real_escape_string($_POST["dataType"][$i]);
    $title = mysql_real_escape_string($_POST["title"][$i]);
    
    $fileData = pathinfo($_FILES["image"]["name"][$i]);
    $fileName = uniqid() . '.' . $fileData['extension'];
    $target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;
    
    if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path)){ // The file is in the images/gallery folder.
    // Insert record into database by executing the following query:
    $sql="INSERT INTO images (data_type, title, file_name) "."VALUES('$dataType','$title','$fileName')";
    $retval = mysql_query($sql);
    
    
    ///NEW
    
    $size = getimagesize($target_path);
    $width=$size[0];
    
    $height=$size[1]; 
    $newwidth = 760;
    $newheight = $height*($newwidth/$width);
    $pic = new Imagick($target_path);//specify name
    $pic->resizeImage($newwidth,$newhight,Imagick::FILTER_LANCZOS,1);
    unlink($target_path);
    $pic->writeImage($target_path);
    $pic->destroy();
    ///NEW
    
    
    echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />
    <a href='index.php'>Add another image</a><br />";
    }
    else
    {
    echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
    }
    }
    } // close your foreach
    ?>
    

    uploader.php原始代码。允许我一次上传4张图片。 WORKS !!!

    <?php
    include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
    
    for($i=0;$i<count($_FILES["image"]["name"]);$i++){
      if($_FILES["image"]["name"][$i] != ''){ // don't insert if file name empty
        $dataType = mysql_real_escape_string($_POST["dataType"][$i]);
        $title = mysql_real_escape_string($_POST["title"][$i]);
    
        $fileData = pathinfo($_FILES["image"]["name"][$i]);
        $fileName = uniqid() . '.' . $fileData['extension'];
        $target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;
    
      if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path)){    // The file is in the images/gallery folder. 
        // Insert record into database by executing the following query:
         $sql="INSERT INTO images (data_type, title, file_name) "."VALUES('$dataType','$title','$fileName')";
         $retval = mysql_query($sql);
    
        echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />
         <a href='index.php'>Add another image</a><br />";
      }
      else
      {
       echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
        }
      }
    } // close your foreach
    ?>
    

    仅供参考,这将允许您为图像指定唯一的名称,调整宽度,但保持正确的宽高比并同时上传多个文件。

    很棒的东西!

2 个答案:

答案 0 :(得分:1)

像这样:

$filelocation='http://help.com/images/help.jpg';
$newfilelocation='http://help.com/images/help1.jpg';

$size = getimagesize($filelocation);
$width=$size[0];//might need to be ['1'] im tired .. :)
$height=$size[1];
// Plz note im not sure of units pixles? & i could have the width and height   confused
//just had some knee surgery so im kinda loopy :) 
$newwidth = 760;
$newheight = $height*($newwidth/$width) 


 $pic = new Imagick( $filelocation);//specify name
 $pic->resizeImage($newwidth,$newhight,Imagick::FILTER_LANCZOS,1);
 //again might have width and heing confused
 $pic->writeImage($newfilelocation);//output name
 $pic->destroy();
 unlink($filelocation);//deletes image

答案 1 :(得分:0)

这是类似的东西,让我们检查尺寸并压缩图像是否太大了。我没有调整它的大小,只需要你获得尺寸并根据需要调整大小。

所有这一切都是如果文件大于250KB将其压缩到85%..

    $bytes = filesize($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);

    //$maxSizeInBytes = 26400; //is 250KB? No? compress it.
    if ($bytes > 26400) {

                $img = new Imagick($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
                $img->setImageCompression(imagick::COMPRESSION_JPEG);
                $img->stripImage();
                $img->setImageCompressionQuality(85);
                $img->writeImage($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);

            }

OR:

            // resize with imagejpeg  ($image, $destination, $quality); if greater than byte size KB
            // Assume only supported file formats on website are jpg,jpeg,png, and gif. (any others will not be compressed)
            $bytes = filesize($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);

            //$maxSizeInBytes = 26400; //is gtr than 250KB? No? compress it.
            if ($bytes > 26400) {

                $info = getimagesize($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
                $quality = 85; //(1-100), 85-92 produces 75% quality 

                if ($info['mime'] == 'image/jpeg') {

                    $image = imagecreatefromjpeg($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
                    imagejpeg($image,$inventory_path.DIRECTORY_SEPARATOR.$this->uploadName,$quality);

                } elseif ($info['mime'] == 'image/gif') { 

                    $image = imagecreatefromgif($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
                    imagejpeg($image,$inventory_path.DIRECTORY_SEPARATOR.$this->uploadName,$quality);

                } elseif ($info['mime'] == 'image/png') { 

                    $image = imagecreatefrompng($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName
                    imagejpeg($image,$inventory_path.DIRECTORY_SEPARATOR.$this->uploadName,$quality);

                }


            }