在上传php-mysql时调整宽度并压缩图像


resize width and compress images on upload php mysql

我有一个客户,他从他的iPhone向我发送带有图像的短信,让我上传到他的图库中。我正在尝试创建一个管理系统,这样我就可以简单地从文本中获取图像,转到iPhone上的管理页面,并将图像直接上传到图库。

这将为我的日常工作日程节省大量时间。

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

  1. 如果可能的话,我想将文件大小压缩到更小的大小,类似于Photoshop中的保存到网页jpg功能。(我得到的大多数图像都在1-3MB左右。我想把它们降到150-500kb左右。)

  2. 我想自动将宽度更改为760px,但保持纵横比,这样图像就不会被压扁。他给我寄来风景画和肖像画。

  3. 它们是iPhone的图像。他们有一个扩展名.JPG(全大写)我想把它改成.JPG(全小写)。这不是一个破坏交易的因素,我只想知道如何做以备将来使用。

这些功能中的任何一个都会很有帮助,但所有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张图片。工作

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

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

太棒了!

像这样:

$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

这里有一些类似的东西,让我们检查图像的大小,如果图像看起来太大,就进行压缩。我没有调整它的大小,这只需要你得到尺寸并根据需要调整大小。

所有这些都是在文件大于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);
            }

或:

            // 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);
                }

            }