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

时间:2021-10-19 09:00:06

I have the following code...

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

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


}

What I'm trying to do is get the uploaded picture, get the extension, check which extension it has, then based on the extension, convert it to jpeg with PHP, then save it as the userid.jpg in the "/bar/" directory. However, nothing is saved.

我要做的是获取上传的图片,获取扩展名,检查它有哪个扩展名,然后根据扩展名,用PHP将其转换为jpeg,然后将其保存为“/ bar /”中的userid.jpg “ 目录。但是,没有任何东西得救。

3 个解决方案

#1


I get solution to convert and save image using php.You can follow this
http://www.kingpabel.com/php-image-resize/

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

#2


PHP < 5.3

PHP <5.3

$fullpath = dirname(__FILE__);

PHP >= 5.3

PHP> = 5.3

$fullpath = __DIR__;

Have a look at predefined constants

看看预定义的常量

#3


This is more robust for getting the file type.

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

Once you have the raw image convert it and save it.

一旦你有原始图像转换并保存它。

Get Upload:

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

Find type:

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

Convert Image to jpg:

将图像转换为jpg:

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

Save:

Where $user is the userid.

其中$ user是userid。

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

}

#1


I get solution to convert and save image using php.You can follow this
http://www.kingpabel.com/php-image-resize/

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

#2


PHP < 5.3

PHP <5.3

$fullpath = dirname(__FILE__);

PHP >= 5.3

PHP> = 5.3

$fullpath = __DIR__;

Have a look at predefined constants

看看预定义的常量

#3


This is more robust for getting the file type.

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

Once you have the raw image convert it and save it.

一旦你有原始图像转换并保存它。

Get Upload:

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

Find type:

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

Convert Image to jpg:

将图像转换为jpg:

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

Save:

Where $user is the userid.

其中$ user是userid。

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

}

相关文章