icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.

» Surpass Web Hosting Forums » Discussions » PHP, MySQL » imageMagick help

PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >>

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
Old December 22nd, 2004, 7:26 PM   #1 (permalink)
Registered User
Seasoned Poster
 
aubrey's Avatar
 
Joined in Oct 2003
41 posts
Gave thanks: 0
Thanked 2 times
imageMagick help

I am currently working on writing an image upload script for my website.

I have gotten the whole thing working, except I need to be able to automatically resize and optumise the images that people upload.

I have been able to accomplish this with GD, but the image quality afterwards was terrible.

The current function I have for saving the images can be seen below. The problem with it is that instead of saving the new image from the old image, it turns the old image into nothing and doesn't write a new one. (Not the desired action )


function imageUploader($imageName,$width=640,$height=480) {

//********************** Verify mime types and determine file extension ***********************

if($_FILES[$imageName]['type'] == "image/pjpeg" || $_FILES[$imageName]['type'] == "image/jpeg") {
$ext = '.jpg';
}elseif($_FILES[$imageName]['type'] == "image/x-png" || $_FILES[$imageName]['type'] == "image/png") {
$ext = '.png';
}elseif($_FILES[$imageName]['type'] == "image/gif") {
$ext = '.gif'; //convert .gif to jpeg!
}
else return false;

//***************************** Do Filename **************************

$badString = array (1,2,3,4,5,6,7,8,9,0,'!',',','-','/',':','_',' ','thumb','.','jpg','png','jpeg','gif','bmp','swf' );
$fileName = str_replace($badString, '', strtolower($_FILES[$imageName]['name'])); // Remove Nonsense
$fileName = str_replace(" ", "_", $fileName); // Convert Spaces
$fileName = '/uploads/images/'.$_SESSION['userId'].$fileName;


while(file_exists($_SERVER['DOCUMENT_ROOT'].$fileName.$ext) || file_exists($_SERVER['DOCUMENT_ROOT'].$fileName.'-original'.$ext)) { //make sure we are not overwriting other files...
$fileName .= chr(mt_rand(97,122)); //add random lowercase letter to filename
}

$temporaryName = $fileName.'-original'.$ext;
$fullTemporaryName = $_SERVER['DOCUMENT_ROOT'].$temporaryName;
$thumbName = $fileName.'-thumb'.$ext;
$fullThumbName = $_SERVER['DOCUMENT_ROOT'].$thumbName;
$fileName .= $ext;
$fullFileName = $_SERVER['DOCUMENT_ROOT'].$fileName;

if(!$_FILES[$imageName]['tmp_name'] || !$_FILES[$imageName]['size']) return 'No File to Upload!';

if (!move_uploaded_file($_FILES[$imageName]['tmp_name'], $fullTemporaryName)) {
return 'Unknown Error.';
}


//********************** GENERATE THUMBNAIL IMAGE *********************

$command = '/usr/bin/convert -size 100x100> '.$fullTemporaryName.' -thumbnail 100x100> -quality 80% '.$fullThumbName;

exec($command);

//************************* GENERATE FULL IMAGE ***********************

$command = '/usr/bin/convert -size '.$width.'x'.$height.'> '.$fullTemporaryName.' -resize '.$width.'x'.$height.'> -quality 70% '.$fullFileName;

exec($command);

//************************* return The Value!!! ***********************

if(is_readable($fullFileName) && is_readable($fullThumbName)) { //ImageMagic Worked!
unlink($fullTemporaryName); //Remove temporary file
return $fileName;
}
else {
return $temporaryName; //IM Didn't work... just give 'em the original image
}

}


Everything I have been reading says this should work, but it doesn't which leads me to think it may be a configuration problem on my server?

Sorry to bug everyone about this, but I have been working on it several days and it's driving me crazy

P.S. Just for the record, the site I am trying to get this working on is http://www.allthingsinteresting.com
aubrey is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old December 22nd, 2004, 9:14 PM   #2 (permalink)
Surpass Fan
Super #1
 
Joined in Aug 2004
Hosted on SH58
1,688 posts
Gave thanks: 6
Thanked 7 times
using the PHP image functions would be easier for this. Reference: http://php.net/image

As for your function, it should go a little something like this:

PHP Code:
<?php
    
function imageUploader($imageName,$width=640,$height=480) {
        
### CONFIGURE THESE ###
        
$th_width "100"// Line # 50
        
$th_height "100"// Line # 50
        ### END CONFIG ###
        //********************** Verify mime types and determine file extension ***********************
        
        
if( ( $_FILES[$imageName]['type'] == "image/pjpeg" ) || ( $_FILES[$imageName]['type'] == "image/jpeg" ) ) {
            
$ext '.jpg';
        } elseif( ( 
$_FILES[$imageName]['type'] == "image/x-png" ) || ( $_FILES[$imageName]['type'] == "image/png" ) ) {
            
$ext '.png';
        }elseif( 
$_FILES[$imageName]['type'] == "image/gif" ) {
            
$ext '.gif';
        } else{
            die( 
'Unsupported filetype!' ); // ::NOTE:: According to your reference, you left out WBMP (*.bmp)
        
}
        
//***************************** Do Filename **************************
        
        
$badString = array ( 1234567890'!'',''-''/'':''_'' ''thumb''.''jpg''png''jpeg''gif''bmp''swf');
        
$fileName str_replace$badString''strtolower$_FILES[$imageName]['name'] ) ); // Remove Nonsense
        
$fileName str_replace(" ""_"$fileName); // Convert Spaces
        
$fileName '/uploads/images/'.$_SESSION['userId'].$fileName;
        
        
        while( 
file_exists$_SERVER['DOCUMENT_ROOT'].$fileName.$ext ) || file_exists$_SERVER['DOCUMENT_ROOT'].$fileName.'-original'.$ext ) ) { //make sure we are not overwriting other files...
            
$fileName .= chrmt_rand97122 ) ); //add random lowercase letter to filename
        
}
        
$temporaryName $fileName.'-original'.$ext;
        
$fullTemporaryName $_SERVER['DOCUMENT_ROOT'].$temporaryName;
        
$thumbName $fileName.'-thumb'.$ext;
        
$fullThumbName $_SERVER['DOCUMENT_ROOT'].$thumbName;
        
$fileName .= $ext;
        
$fullFileName $_SERVER['DOCUMENT_ROOT'].$fileName;
        
        if( !
$_FILES[$imageName]['tmp_name'] || !$_FILES[$imageName]['size'] ){
            return 
'No File to Upload!';
        }
        if ( !
move_uploaded_file$_FILES[$imageName]['tmp_name'], $fullTemporaryName ) ) {
            return 
'Unknown Error.';
        }
        
        
        
//********************** GENERATE THUMBNAIL IMAGE *********************
        
        /*$command = '/usr/bin/convert -size 100x100> '.$fullTemporaryName.' -thumbnail 100x100> -quality 80% '.$fullThumbName;
        
        exec($command);*/
        
        
$th_img imagecreatetruecolor$th_height$th_width );
        
$th_from imagecreatefromjpeg$_SERVER['DOCUMENT_ROOT'].$fileName );
        
$th_attr getimagesize$_SERVER['DOCUMENT_ROOT'].$fileName );
        
imagecopyresized$th_img$th_from0000$th_width$th_height$th_attr[0], $th_attr[1] );
        switch( 
$ext ){
            case 
$ext == ".jpg" imagejpeg$th_img$_SERVER['DOCUMENT_ROOT']."/thumb-".$fileName ); break;
            case 
$ext == ".png" imagepng$th_img$_SERVER['DOCUMENT_ROOT']."/thumb-".$fileName ); break;
            case 
$ext == ".gif" imagegif$th_img$_SERVER['DOCUMENT_ROOT']."/thumb-".$fileName ); break;
            case default : 
imagejpeg$th_img ); break;
        }
        
imagedestroy$th_img );
        
        
//************************* GENERATE FULL IMAGE ***********************
        
        /* $command = '/usr/bin/convert -size '.$width.'x'.$height.'> '.$fullTemporaryName.' -resize '.$width.'x'.$height.'> -quality 70% '.$fullFileName;
        
        exec($command); */
        
        // Aren't the files already made? If not, you can modify lines 50-60 and copy them down here to remake the image ;)
        
        //************************* return The Value!!! ***********************
        
        /*if( is_readable( $fullFileName ) && is_readable( $fullThumbName ) ) { //ImageMagic Worked!
            unlink($fullTemporaryName); //Remove temporary file
            return $fileName;
        } else {
            return $temporaryName; //IM Didn't work... just give 'em the original image
        }*/
        
        // Do not think you need this.
        
    
}
?>
NOTE: since this is not on my server, I have no way of testing this. However, post any errors and I will try and remedy them
__________________
- Evan Charlton | [site] | Server - SH58
Kickersny.com is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old December 23rd, 2004, 3:32 PM   #3 (permalink)
Registered User
Seasoned Poster
 
aubrey's Avatar
 
Joined in Oct 2003
41 posts
Gave thanks: 0
Thanked 2 times
It Works!

Thanks for your reply!

Following is the new image upload function. I have rewritten it according to your suggestions. I would rather have this working with imagemagick, but since it works I can't complain!

PHP Code:
    function imageUploader($imageName,$width=640,$height=480) {
        
//********************** Verify mime types and determine file extension ***********************
        
        
if( ( $_FILES[$imageName]['type'] == "image/pjpeg" ) || ( $_FILES[$imageName]['type'] == "image/jpeg" ) ) {
            
$type 'imagecreatefromjpeg';
        } elseif( ( 
$_FILES[$imageName]['type'] == "image/x-png" ) || ( $_FILES[$imageName]['type'] == "image/png" ) ) {
            
$type 'imagecreatefrompng';
        }elseif( 
$_FILES[$imageName]['type'] == "image/gif" ) {
            
$type 'imagecreatefromgif';
        } else{
            return 
'The file you supplied was not a valid image file!'
        }
        
//***************************** Do Filename **************************
        
        
$badString = array ( 1234567890'!'',''-''/'':''_'' ''thumb''.''jpg''png''jpeg''gif''bmp''swf');
        
$fileName str_replace$badString''strtolower$_FILES[$imageName]['name'] ) ); // Remove Nonsense
        
$fileName str_replace(" ""_"$fileName); // Convert Spaces
        
$fileName '/uploads/images/'.$_SESSION['userId'].$fileName;
        
        
        while( 
file_exists$_SERVER['DOCUMENT_ROOT'].$fileName.'.jpg' ) || file_exists$_SERVER['DOCUMENT_ROOT'].$fileName.'-original'.'.jpg' ) ) { //make sure we are not overwriting other files...
            
$fileName .= chrmt_rand97122 ) ); //add random lowercase letter to filename
        
}
        
$temporaryName $fileName.'-original.jpg';
        
$fullTemporaryName $_SERVER['DOCUMENT_ROOT'].$temporaryName;
        
$thumbName $fileName.'-thumb.jpg';
        
$fullThumbName $_SERVER['DOCUMENT_ROOT'].$thumbName;
        
$fileName .= '.jpg';
        
$fullFileName $_SERVER['DOCUMENT_ROOT'].$fileName;
        
        if( !
$_FILES[$imageName]['tmp_name'] || !$_FILES[$imageName]['size'] ){
            return 
'No File to Upload!';
        }
        if ( !
move_uploaded_file$_FILES[$imageName]['tmp_name'], $fullTemporaryName ) ) {
            return 
'Unknown Error.';
        }
        
        
$currentimagesize getimagesize$fullTemporaryName );
        
$image_width $currentimagesize[0];
        
$image_height$currentimagesize[1];
        
        
        
        if ((
$image_height $width) || ($image_width $height)) {   
            if (
$image_height $image_width
            { 
              
$sizefactor = (double) ($height $image_height);
            } 
            else 
            {
              
$sizefactor = (double) ($width $image_width) ;
            }
            
$newWidth = (int) ($image_width $sizefactor);
            
$newHeight = (int) ($image_height $sizefactor);
        }
        else {
            
$newWidth $image_width;
            
$newHeight $image_height;
        }
        
        
        
$full_img imagecreatetruecolor$newWidth$newHeight );
        
$full_from $type$fullTemporaryName );
        
$full_attr getimagesize$fullTemporaryName );
        
imagecopyresized$full_img$full_from0000$newWidth$newHeight$image_width$image_height );
        
imagejpeg$full_img$fullFileName,80 );
        
imagedestroy$full_img );
        
        
        
        
        if ((
$image_height 100) || ($image_width 100)) {   
            if (
$image_height $image_width
            { 
              
$sizefactor = (double) (100 $image_height);
            } 
            else 
            {
              
$sizefactor = (double) (100 $image_width) ;
            }        
            
            
$newWidth = (int) ($image_width $sizefactor);
            
$newHeight = (int) ($image_height $sizefactor);
        }
        else {
            
$newWidth $image_width;
            
$newHeight $image_height;
        }
    

        
$th_img imagecreatetruecolor$newWidth$newHeight );
        
$th_from $type$fullTemporaryName );
        
imagecopyresized$th_img$th_from0000$newWidth$newHeight$image_width$image_height );
        
imagejpeg$th_img$fullThumbName,70 );
        
imagedestroy$th_img );
        
        
   
        
//************************* return The Value!!! ***********************
        
        
if( is_readable$fullFileName ) && is_readable$fullThumbName ) ) { //ImageMagic Worked!
            
unlink($fullTemporaryName); //Remove temporary file
            
return $fileName;
        } else {
            return 
$temporaryName//Resize Didn't work... just give 'em the original image
        
}
    } 
Thanks for the idea. If you want to see this script in action, visit http://www.allthingsinteresting.com/...eds/submit.htm
Try inserting pictures into the textarea!
aubrey is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On