• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Why can't images be resized with PHP?

I have been told by dozens of developers, both from academic circles and practical circles, that PHP cannot be used to resize an image without the assistance of an extension like GD or without shelling to a third-party application like ImageMagick.

I do not understand why PHP cannot do this.

Couldn't PHP place the binary code of an image into a string, then process that binary code to resize an image?  Am I missing something?
0
hankknight
Asked:
hankknight
  • 3
  • 2
3 Solutions
 
DavidSingletonCommented:
I believe the simple answer is because they didn't need too.  There was already a library out that that would resize the images, so they made use of them.  Also, i imagine readying the binary code of a 100MB image could get very very hairy.  It just seems impractical to me.
0
 
agamalCommented:
you can resize images using php



<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);
 
              $imagepath = $imagename;
              $save = "images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ;
 
              $modwidth = 150;
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
 
              imagejpeg($tn, $save, 100) ;
 
              $save = "images/sml_" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ;
 
              $modwidth = 80;
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
 
              imagejpeg($tn, $save, 100) ;
            echo "Large image: <img src='images/".$imagepath."'><br>";
            echo "Thumbnail: <img src='images/sml_".$imagepath."'>";
 
          }
        }
?>

Open in new window

0
 
hankknightAuthor Commented:
agamal, your code is NOT pure PHP.  It requires the GD extension to be installed.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
agamalCommented:
as far as i know you can do it with GD.  PHP doesn't have any built-in image functions.
 
 If you have ImageMagick (ImageMagick.org), you can use that too.  And you may be
 able to use GIMP, but I'm not sure.
0
 
DavidSingletonCommented:
Agamal i think he understands that, he was just curious as to why it is php doesn't have any built in image manipulation.

I think honestly, its because that was way beyond the scope of php when it was first built.  How many websites run php, and how many of them need to resize images, I bet the number is quite small compared to the number of users.
0
 
agamalCommented:
totally agree .... : - )
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now