我想将图像压缩到特定目录

时间:2016-11-16 06:37:44

标签: php

图像已压缩,但它会存储在脚本文件夹的哪个位置,但我想存储特定给定目录的图像。

$name = ''; $type = ''; $size = ''; $error = '';
function compress_image($source_url, $destination_url, $quality) {

      $info = getimagesize($source_url);

          if ($info['mime'] == 'image/jpeg')
          $image = imagecreatefromjpeg($source_url);

          elseif ($info['mime'] == 'image/gif')
          $image = imagecreatefromgif($source_url);

          elseif ($info['mime'] == 'image/png')
          $image = imagecreatefrompng($source_url);

          imagejpeg($image, $destination_url, $quality);
          return $destination_url;
        }
     if ($_POST) {
            $upload_image = $_FILES["file"][ "name" ];
            if ($_FILES["file"]["error"] > 0)
            {
            $error = $_FILES["file"]["error"];
            }
            else if (($_FILES["file"]["type"] == "image/gif") ||
            ($_FILES["file"]["type"] == "image/jpeg") ||
            ($_FILES["file"]["type"] == "image/png") ||
            ($_FILES["file"]["type"] == "image/pjpeg")) {

            $url = $upload_image;
           $filename = compress_image($_FILES["file"]["tmp_name"], $upload_image, 80);


           // if(move_uploaded_file($image, $target_dir.$url))
            {
              echo "uploaded success";
            }
        }else {
            $error = "Uploaded image should be jpg or gif or png";
        }
        }

1 个答案:

答案 0 :(得分:0)

替换

imagejpeg($image, $destination_url, $quality);

imagejpeg($image, "<Your Path>/".$destination_url, $quality);

例如

imagejpeg($image, "uploads/".$destination_url, $quality);