在PHP中转换和保存上传的图像?

时间:2015-05-11 08:57:28

标签: php image upload save converter

我有以下代码......

$current_full_path = realpath(dirname(__FILE__) . '/..');
$user_profile_picture_filename = $current_full_path . "/profile_pictures/" . $userid . ".jpg";

$user_profile_picture_submit = $_POST['user_profile_picture_submit'];

if (isset ($user_profile_picture_submit)) {

    $user_profile_image = $_FILES['user_profile_image']['name'];
    $temp_profile_picture = $_FILES['user_profile_image']['tmp_name'];

    $profile_picture_extension_array = explode( ".", $user_profile_image); 
    $profile_picture_extension_case_insensitive = end($profile_picture_extension_array);
    $profile_picture_extension = strtolower($profile_picture_extension_case_insensitive);

    if ($profile_picture_extension == "png") {

        $temp_profile_picture_converted = imagecreatefrompng($temp_profile_picture);

    } elseif ($profile_picture_extension == "bmp") {

        $temp_profile_picture_converted = imagecreatefromwbmp($temp_profile_picture);

    } elseif ($profile_picture_extension == "gif") {

        $temp_profile_picture_converted = imagecreatefromgif($temp_profile_picture);

    } elseif (($profile_picture_extension == "jpg") || ($profile_picture_extension == "jpeg")) {

        $temp_profile_picture_converted = imagecreatefromjpeg($temp_profile_picture);

    } 

    imagejpeg($temp_profile_picture_converted, $user_profile_picture_filename, '75');
    imagedestroy($temp_profile_picture_converted);


}

我尝试做的是获取上传的图片,获取扩展程序,检查扩展程序,然后根据扩展程序,使用PHP将其转换为jpeg,然后将其保存为userid.jpg in " / bar /"目录。但是,没有任何东西可以保存。

3 个答案:

答案 0 :(得分:0)

我得到了使用php转换和保存图像的解决方案。你可以按照这个 http://www.kingpabel.com/php-image-resize/

答案 1 :(得分:0)

PHP< 5.3

$fullpath = dirname(__FILE__);

PHP> = 5.3

$fullpath = __DIR__;

查看predefined constants

答案 2 :(得分:0)

这对于获取文件类型更为健壮。

原始图像转换后保存并保存。

获取上传:

if( is_uploaded_file($_FILES['image1']['tmp_name']) || !($_FILES['image1']['error'] !== UPLOAD_ERR_OK)){
  $save = false;

查找类型:

  switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = getimagesize($_FILES['image1']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = imagecreatefromjpeg($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = imagecreatefrompng($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = imagecreatefromgif($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image1']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
}

将图片转换为jpg:

 if ($save === true){
   ob_start();
   imagejpeg($image, NULL, 70);
   $jpg = ob_get_clean();

保存

$user是用户ID。

   $fp = fopen("/home/user/public_html/bar/$user.jpg",'w');
   fwrite($fp,$jpg);
   fclose($fp);
 }

}