1

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

这将在我的日常工作安排中节省大量时间。

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

  1. 如果可能,我想将文件大小压缩到更小的大小,类似于 Photoshop 中的保存到 web jpg 功能。(我得到的大多数图像都在 1-3 MB 左右。我希望将它们降低到最大 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
?>

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

很棒的东西!

4

2 回答 2

2

像这样:

$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
于 2013-07-15T06:13:33.817 回答
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);

            }

或者:

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

                }


            }
于 2016-06-29T00:45:58.730 回答