Jae
Jae

Reputation: 321

Trying to resize an image using PHP

Anybody know what's wrong with this code? Instead of giving me an actual image, it's giving me an image error. I think I set all the functions correctly and everything. Any suggestions would be awesome.

<?php
function resizeImage($filename, $max_width, $max_height) {
  list($orig_width, $orig_height) = getimagesize($filename);

  $width = $orig_width;
  $height = $orig_height;

  # taller
  if ($height > $max_height) {
    $width = ($max_height / $height) * $width;
    $height = $max_height;
  }

  # wider
  if ($width > $max_width) {
    $height = ($max_width / $width) * $height;
    $width = $max_width;
  }

  $image_p = imagecreatetruecolor($width, $height);

  $image = imagecreatefromjpeg($filename);

  imagecopyresampled($image_p, $image, 0, 0, 0, 0, 
                     $width, $height, $orig_width, $orig_height);

  return $image_p;
}

$directory = "uploads/";
$images = glob($directory."*");

foreach($images as $image) {

  // Set a maximum height and width
  $width = 200;
  $height = 200;
  $newImage = resizeImage($image, $width, $height);

  echo '<img src="'.$newImage.'" /><br />';
}      
?>

Upvotes: 3

Views: 75

Answers (1)

Susheel Singh
Susheel Singh

Reputation: 3854

if you dont want to save the image, then you can use the below code.

<?php
    function resizeImage($filename, $max_width, $max_height) {
        list($orig_width, $orig_height) = getimagesize($filename);

        $width = $orig_width;
        $height = $orig_height;

        # taller
        if ($height > $max_height) {
            $width = ($max_height / $height) * $width;
            $height = $max_height;
        }

        # wider
        if ($width > $max_width) {
            $height = ($max_width / $width) * $height;
            $width = $max_width;
        }

        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
        ob_start();
        imagejpeg($image_p); 
        $imgSrc = 'data:image/jpeg;base64,'.base64_encode(ob_get_clean());
        return $imgSrc;
    }

    $directory = "uploads/";
    $images = glob($directory."*");

    foreach($images as $image) {
        // Set a maximum height and width
        $width = 200;
        $height = 200;
        $imgSrc = resizeImage($image, $width, $height);
        echo '<img src="'.$imgSrc.'" /><br />';
    }      
?>

if you want to save the image, then you have to create some other folder like resized and save cropped images there and link it as given below.(don't change your old code and add this)

<?php    
    $directory = "uploads/";
    $images = glob($directory."*");

    $i=1;//added
    foreach($images as $image) {
       // Set a maximum height and width
        $width = 200;
        $height = 200;
        $newImage = resizeImage($image, $width, $height);
        imagejpeg($newImage,"resized/newimage".$i.".jpg");  //added
        echo '<img src="resized/newimage'.$i.'.jpg" /><br />';//modified
        $i++;//added
    }      
?>

Upvotes: 1

Related Questions