How to image resize using php?

613

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 .

Here is my PHP code.

$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];
546

Answer

Solution:

<?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";
812

Answer

Solution:

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.

People are also looking for solutions to the problem: excel - Performance/Load Testing PHP / Coldfusion Spreadsheet Web Processing?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.