Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old May 11, 2006, 09:28   #1
hisham777
SitePoint Guru
 
hisham777's Avatar
 
Join Date: Dec 2005
Posts: 802
Question Problem with watermark

hello

am facing a problem with image upload
the script does this
upload image
create a themb
resize the image then add a water mark on top of the image using GD 2

the bog i have is the image will turn black
and the thumb is normal

and i get this

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in E:\mysite\script\photoalbum\test\upload.php on line 195


PHP Code:

 <?php
    
include("config.php");

    
// initialization
$result_final = "";
$counter = 0;

// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-png' => 'png');
        
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG');

// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];
$photoFileName= $_FILES['photo_filename'];
        
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];

while(
$counter <= count($photos_uploaded) )
        {
                if(
$photos_uploaded['size'][$counter] > 0)
                {
                        if(!
array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                        {
                                
$result_final .= "File ".($counter+1)." is not a photo!<br />";
                        }else{
                        
                                
mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );
                                
$new_id = mysql_insert_id();
                                
$filetype = $photos_uploaded['type'][$counter];
                                
$extention = $known_photo_types[$filetype];
                                
$filename = $new_id.".".$extention;

                                
mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );
                        
// Store the orignal file
  
copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);

// Let's get the original image size
                                
$size = GetImageSize( $images_dir."/".$filename );

// First Create Thumbnail!
// Thumbnail Settings
        
$Config_tbwidth_wide = 150; // width of wide image
        
$Config_tbheight_wide = 125; // height of wide image

        
$Config_tbwidth_tall = 125; // width of tall image
        
$Config_tbheight_tall = 150; // height of tall image

// The Code
        
if($size[0] > $size[1]){
            
$thumbnail_width = $Config_tbwidth_wide;
            
$thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);

            if(
$thumbnail_height > $Config_tbheight_wide){
                
$thumbnail_height = $Config_tbheight_wide;
                
$thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
            }
        }else{
            
$thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
            
$thumbnail_height = $Config_tbheight_tall;

            if(
$thumbnail_width > $Config_tbwidth_tall){
                
$thumbnail_width = $Config_tbwidth_tall;
                
$thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
            }
        }

// Build Thumbnail with GD 2.x.x, you can use the other described methods too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;

// Read the source file
$source_handle = $function_to_read ( $images_dir."/".$filename );

if(
$source_handle){
// Let's create a blank image for the thumbnail
$destination_handle = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height );

// Now we resize it
ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
}

// Let's save the thumbnail
$function_to_write( $destination_handle, $images_dir."/tb_".$filename, 100 );
ImageDestroy($destination_handle );


if (
$size[0] > '600'){
        
$Config_width_wide = 600; // width of wide image
        
$Config_height_wide = 525; // height of wide image

        
$Config_width_tall = 525; // width of tall image
        
$Config_height_tall = 600; // height of tall image

// The Code
        
if($size[0] > $size[1]){
            
$image_width = $Config_width_wide;
            
$image_height = (int)($Config_width_wide * $size[1] / $size[0]);

            if(
$image_height > $Config_height_wide){
                
$image_height = $Config_height_wide;
                
$image_width = (int)($Config_height_wide * $size[0] / $size[1]);
            }
        }else{
            
$image_width = (int)($Config_height_tall * $size[0] / $size[1]);
            
$image_height = $Config_height_tall;

            if(
$image_width > $Config_width_tall){
                
$image_width = $Config_width_tall;
                
$image_height = (int)($Config_width_tall * $size[1] / $size[0]);
            }
        }

// Build image with GD 2.x.x, you can use the other described methods too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;

// Read the source file
$source_handle = $function_to_read ( $images_dir."/".$filename );

$watermark_width = 100;
$watermark_height = 100;
$dest_x = ($size[0] - $watermark_width) / 2;
$dest_y = ($size[1] - $watermark_height) / 2;
//stick a watermark on from an image
$watermark = imagecreatefrompng('water1.png');

               
               
$source_handle  = imagecopymerge($source_handle , $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);




if(
$source_handle){
// Let's create a blank image for the image
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

// Now we resize it
//  its her
ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
}


// Let's save the image
$function_to_write( $destination_handle, $images_dir."/".$filename, 90 );
ImageDestroy($destination_handle );

}

$result_final .= "<img src='".$images_dir. "/tb_".$filename."' /><br />File ".($counter+1)." Added Successfully!<br /><br />";

}
}
$counter++;
}

    
// Print Result
echo <<<__HTML_END

<html>
<head>
    <title>Photos uploaded</title>
</head>
<body>
    $result_final
</body>
</html>

__HTML_END;
?>



thanks for the help
hisham777 is offline   Reply With Quote
Old May 11, 2006, 10:29   #2
hisham777
SitePoint Guru
 
hisham777's Avatar
 
Join Date: Dec 2005
Posts: 802
never mind ill just live without it
thanks any way
hisham777 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 17:33.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved