How to image resize using php?

前端 未结 2 2076
有刺的猬
有刺的猬 2021-01-17 05:46

Image retrieve from database and copy image in another folder with same name and image resize with 150x150.

I don\'t know where is actuall problem in fatching image

相关标签:
2条回答
  • 2021-01-17 06:43

    I think this will help you.

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

    Your image will be saved in 'user_data/thumbnail' folder.

    0 讨论(0)
  • 2021-01-17 06:46
    <?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
    ?>
    

    change you php code like below: try it

        $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";
    
    0 讨论(0)
提交回复
热议问题