9

我只是上传:

$path = "上传/";

$actual_image_name = time().$session_id.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
move_uploaded_file($tmp, $path.$actual_image_name);

我怎样才能添加这个功能imagejpeg?我想总是以 JPG 格式保存图像。

4

2 回答 2

15

试试这个:

<?php

$path = "uploads/";

$img = $_FILES['photoimg']['tmp_name'];
$dst = $path . $_FILES['photoimg']['name'];

if (($img_info = getimagesize($img)) === FALSE)
  die("Image not found or not an image");

$width = $img_info[0];
$height = $img_info[1];

switch ($img_info[2]) {
  case IMAGETYPE_GIF  : $src = imagecreatefromgif($img);  break;
  case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
  case IMAGETYPE_PNG  : $src = imagecreatefrompng($img);  break;
  default : die("Unknown filetype");
}

$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($tmp, $dst.".jpg");

?>
于 2013-01-27T18:22:15.940 回答
2

确保已安装 GD 库并使用它将 gif 或 png 转换为 jpg 之类的

    if ($_FILES["photoimg"]["type"] == "image/jpeg" || $_FILES["photoimg"]["type"] == "image/png" || $_FILES["photoimg"]["type"] == "image/gif" )
    {
      if ($_FILES["photoimg"]["type"] == "image/png" || $_FILES["photoimg"]["type"] == "image/gif" )
      {
       imagejpeg(imagecreatefromstring(file_get_contents($_FILES["photoimg"]["tmp_name"])), "converted.jpg");
       $actual_image_name = time().$session_id.".".$ext;
       move_uploaded_file("converted.jpg", $path.$actual_image_name);
      }
      else
      {
        $actual_image_name = time().$session_id.".".$ext;
        $tmp = $_FILES['photoimg']['tmp_name'];
        move_uploaded_file($tmp, $path.$actual_image_name);
      }
    }
于 2013-01-27T18:02:41.867 回答