Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to resize an image using PHP

Tags:

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 />';
}      
?>
like image 616
Jae Avatar asked Feb 19 '26 00:02

Jae


1 Answers

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
    }      
?>
like image 69
Susheel Singh Avatar answered Feb 21 '26 12:02

Susheel Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!