如何使用PHP调整图像大小?

时间:2014-07-18 05:29:28

标签: php

从数据库中检索图像并在具有相同名称的另一个文件夹中复制图像,并使用150x150调整图像大小。

我不知道在肥胖图像中究竟存在什么问题。

这是我的PHP代码。

$s=mysql_query("select * from photo_gallery where image_id = '".$image_id."'");
$r = mysql_fetch_array($s);

echo $filename = 'user_data/'.$r['image_name'];
$filname1 = explode("/",$filename);

echo $filname1[0]; echo "<br>";
echo $filename = $filname1[1];

2 个答案:

答案 0 :(得分:2)

<?php
function image_resize($imageName,$newName,$newWidth,$newHeight)
{
      $imginfo = getimagesize($imageName); //get information about image
      $type = $imginfo[2]; //third element of array is image type
      if( $type == IMAGETYPE_JPEG ) {  //if image is jpeg type
         $image = imagecreatefromjpeg($imageName);
      } elseif( $type == IMAGETYPE_GIF ) {  // if image is gif type
         $image = imagecreatefromgif($imageName);
      } elseif( $type == IMAGETYPE_PNG ) {  //if image is png type
         $image = imagecreatefrompng($imageName);
      }
      $new_img = imagecreatetruecolor($newWidth, $newHeight); //create a new image
      imagecopyresampled($new_img, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image)); 
      imagejpeg($new_img,$newName,75); //save the image as jpeg
}
//image_resize("original.jpg","output.jpg","150","150"); //how to use
?>

改变你的PHP代码,如下所示:试试吧

    $s=mysql_query("select * from photo_gallery where image_id = '".$image_id."'");
    $r = mysql_fetch_array($s);

    $filename = 'user_data/'.$r['image_name'];

    image_resize($filename,$filename."_thumbnail.jpg","150","150"); //how to use

    echo 'orginial image is '.$filename.'<br> thumbnail is '.$filename."_thumbnail.jpg";

答案 1 :(得分:0)

我认为这会对你有帮助。

             $path = getcwd();
             $oldpic = $path.'/user_data/'.$r['image_name'];
             $array = explode("/",$oldpic);
             $count = count($array);
             $name = $array[$count-1];

             $src = $oldpic;
             $dest = $path."/user_data/thumbnail/".$name;

             //Genrating the image from there extension
             $size = getimagesize($src);
             switch($size["mime"]){

                        case "image/jpeg":
                          $source_image = imagecreatefromjpeg($src); //jpeg file
                        break;

                        case "image/gif":
                          $source_image = imagecreatefromgif($src); //gif file
                        break;

                        case "image/png":
                          $source_image = imagecreatefrompng($src); //png file
                        break;

                        default:
                          $source_image=false;
                        break;
             }
             $width = imagesx($source_image);
             $height = imagesy($source_image);
             $newwidth=150;
             $newheight=150;
             $virtual_image = imagecreatetruecolor($newwidth, $newheight);
             imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
             imagejpeg($virtual_image,$dest,100);

您的图片将保存在“user_data / thumbnail”文件夹中。