changing image type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edzin
Forum Newbie
Posts: 3
Joined: Fri Jan 30, 2009 10:35 am

changing image type

Post by edzin »

I have the below script which uploads an image and resizes it to 4 specified heights. Right now it only works for jpgs, but I want to be able to upload any image type and have the file converted to jpg before it is copied and resized. I have ImageMagick installed but can't seem to get it to work. Thanks for your help!

Code: Select all

<?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/high/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modheight = 800; 
 
              $diff = $height / $modheight;
 
              $modwidth = $width / $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/medium/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modheight = 600; 
 
              $diff = $height / $modheight;
 
              $modwidth = $width / $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/low/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modheight = 450; 
 
              $diff = $height / $modheight;
 
              $modwidth = $width / $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/tiny/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modheight = 300; 
 
              $diff = $height / $modheight;
 
              $modwidth = $width / $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: <br /><br /><img src='images/high/".$imagepath."'><br /><br /><hr />"; 
            echo "Medium image: <br /><br /><img src='images/medium/".$imagepath."'><br /><br /><hr />";
            echo "Small image: <br /><br /><img src='images/low/".$imagepath."'><br /><br /><hr />"; 
            echo "Tiny image: <br /><br /><img src='images/tiny/".$imagepath."'>"; 
 
          }
        }
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: changing image type

Post by requinix »

If you have ImageMagick installed then you should definitely use it. What code do you have that's not working?
edzin
Forum Newbie
Posts: 3
Joined: Fri Jan 30, 2009 10:35 am

Re: changing image type

Post by edzin »

that's the thing. i don't know how to go about using it. i tried to look at the website but since i don't know php very well i couldn't figure it out.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: changing image type

Post by requinix »

Have you looked around here? Perhaps here?
Post Reply