Help resize png-jpg or jpeg-jif

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
killingbegin
Forum Newbie
Posts: 20
Joined: Sun Feb 08, 2009 5:07 am

Help resize png-jpg or jpeg-jif

Post by killingbegin »

Hi this is my code the page say if i pressed the submit do some php code
else show the html that have the upload input and an input bbox if i want to change the name

it takes the image name and type
if the text is empry then it use the upload name else it use the name from the text."the file type"
and it copy to a folder name photos/

then i got a resizer that resize the foto that i use it for thumb
there i have and if statment that

Code: Select all

 
if($file_type == "image/jpeg" || $file_type == "image/jpg")
            {
                $src = imagecreatefromjpeg($uploadedfile);            }
 
            else
            if($file_type == "image/png")
            {
                $src = imagecreatefrompng($uploadedfile);
            }
            else
            if($file_type == "image/gif")
            {
                $src = imagecreatefromgif($uploadedfile);
            }
            else            
            {
                echo "I Dont allow <_". $file_type."_> images";                
            }
 
 
if the $src is jpeg it will do the resize
if it is jpg-png-bmp-jif is how me errors anyone that can help?

Code: Select all

 
<?php
    
        if($_POST['submit'])
        {
            //perno tto name ths photo
            $file_name = $HTTP_POST_FILES['ufile']['name'];
            $file_type = $HTTP_POST_FILES['ufile']['type'];
            //to onoma ths eikonas
            $new_name = $_POST['name'];
            if($new_name == "")
            {
                $new_file_name = $file_name;
            }
            else
            {
            $new_file_name=$new_name.filetype;
            }
            //dialego to path p tha th valo
            $path= "photos/".$new_file_name;
 
            if($ufile !=none)
            {
                if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
                {
                    echo "File Name : ".$new_file_name;
                }
                else
                {
                    echo "Error";
                }
             
            }
            
            
            
            if($file_type == "image/jpeg" || $file_type == "image/jpg")
            {
                $src = imagecreatefromjpeg($uploadedfile);
            }
 
            else
            if($file_type == "image/png")
            {
                $src = imagecreatefrompng($uploadedfile);
            }
            else
            if($file_type == "image/gif")
            {
                $src = imagecreatefromgif($uploadedfile);
            }
            else            
            {
                echo "I Dont allow <_". $file_type."_> images";
                
            }
 
        
            echo "<br>".$file_type;
            
            // This is the temporary file created by PHP
            $uploadedfile = $file_name;
            
            // Create an Image from it so we can do the resize
            
            
            // Capture the original size of the uploaded image
            list($width,$height)=getimagesize($uploadedfile);
            
            // For our purposes, I have resized the image to be
            // 600 pixels wide, and maintain the original aspect
            // ratio. This prevents the image from being "stretched"
            // or "squashed". If you prefer some max width other than
            // 600, simply change the $newwidth variable
            $newwidth=150;
            $newheight=($height/$width)* $newwidth;
            $tmp=imagecreatetruecolor($newwidth,$newheight);
         
            
            
            // this line actually does the image resizing, copying from the original
            // image into the $tmp image
            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
            
            // now write the resized image to disk. I have assumed that you want the
            // resized, uploaded image file to reside in the ./images subdirectory.
            $filename = "thumbs/". $uploadedfile;
            imagejpeg($tmp,$filename,100);
        
 
                        
        }
        else
        {
                        
                    
 
 
?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
 
<form action="import.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="ufile" type="file" id="ufile" size="29" /><br>
<input name="name" type="text" id="name" size="29" />
<input name="submit" type="submit" class="buttons" id="submit" value="Submit" />
</form>
<?php
}
?>
 
jceresini
Forum Newbie
Posts: 8
Joined: Fri Mar 27, 2009 6:03 pm

Re: Help resize png-jpg or jpeg-jif

Post by jceresini »

It sounds to me like your version of GD doesn't support other file types. You may want to create a phpinfo page and scroll to the section that shows details of the GD module. Heres an example of one:

http://joeceresini.com/info.php#module_gd

If you do not have support for some of those formats you will probably have to recompile the GD module. Information about that can be found here:
http://us2.php.net/manual/en/image.requirements.php

Joe Ceresini
Network Engineer
jceresini@hostmysite.com
HostMySite.com
Post Reply