I can upload and resize image - I need to apply a PNG or GIF

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
kagronick
Forum Newbie
Posts: 4
Joined: Thu Jul 30, 2009 12:30 am

I can upload and resize image - I need to apply a PNG or GIF

Post by kagronick »

There is a script here I would like to use:
http://bokehman.com/watermark

The error I am getting is "Cannot modify header information - headers already sent"

Here is the code so far:

Code: Select all

<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 = 400; 
                                                         
              $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 "<img src='images/".$imagepath."'><br>"; 
        
          }
        }
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I can upload and resize image - I need to apply a PNG or GIF

Post by requinix »

Don't know where it is but the error is not coming from that bit of code.

Look for session_start() or header() somewhere.
kagronick
Forum Newbie
Posts: 4
Joined: Thu Jul 30, 2009 12:30 am

Re: I can upload and resize image - I need to apply a PNG or GIF

Post by kagronick »

Heres where I'm at now. Yes I know theres some very stupid redundant code. I'm not skilled in php. I got it to put text on the image. Now I want it to apply a image over it. The line imagecopy($image2, $watermark, 0, 0, 0, 0, 400, 400); should do it. The error I get is

Warning: imagecopy(): supplied argument is not a valid Image resource in /home/qualityc/public_html/pupeframes/index.php on line 71.

Code: Select all

<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 = 400; 
                                                         
              $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) ; 
        
 
function addText ($SourceFile, $WaterMarkText, $DestinationFile) {
   list($width, $height) = getimagesize($SourceFile);
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = 'arial.ttf';
   $font_size = 30;
   imagettftext($image_p, $font_size, 0, ($width / 4), ($height - 30), $black, $font, $WaterMarkText);
   if ($DestinationFile<>'') {
      imagejpeg ($image_p, $DestinationFile, 100);
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
   };
   imagedestroy($image);
   imagedestroy($image_p);
};
addText ('images/'.$imagepath, 'test string', 'images/'.$imagepath);        
 
if($filetype == ".gif")  $image2 = @imagecreatefromgif('images/'.$imagepath);  
if($filetype == ".jpg")  $image2 = @imagecreatefromjpeg('images/'.$imagepath);  
if($filetype == ".png")  $image2 = @imagecreatefrompng('images/'.$imagepath);  
$watermark = @imagecreatefrompng('images/watermark.png'); 
imagecopy($image2, $watermark,  0, 0, 0, 0, 400, 400); 
            echo "<img src='images/".$imagepath."'><br>"; 
        
          }
        }
?>
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: I can upload and resize image - I need to apply a PNG or GIF

Post by turbolemon »

It's likely that your assignment to $image2 isn't working. Remove the error suppression ("@") from lines 67-69 and see what errors show up.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I can upload and resize image - I need to apply a PNG or GIF

Post by pickle »

The error stems from your call to header(). It looks like part of this code is set up to simply output the image to the screen. When it does that, there can be no previous output (namely the <form> you've got).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kagronick
Forum Newbie
Posts: 4
Joined: Thu Jul 30, 2009 12:30 am

Re: I can upload and resize image - I need to apply a PNG or GIF

Post by kagronick »

I found this script that im trying to incorportate

Code: Select all

$sourceImage = '/Library/WebServer/Documents/images/Lucy.jpg';
$watermarkImage = '/Library/WebServer/Documents/images/saturn.png';
$x = 100;
$y = 100;
 
header('content-type: image/jpeg');
 
$img = imagecreatefromjpeg($sourceImage);
imagesetbrush($img, imagecreatefrompng($watermarkImage));
imageline($img, $x, $y, $x, $y, IMG_COLOR_BRUSHED);
imagejpeg($img);
 
This is my full script:

Code: Select all

 
<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 = 400; 
                                                         
              $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) ; 
        
 
function addText ($SourceFile, $WaterMarkText, $DestinationFile) {
   list($width, $height) = getimagesize($SourceFile);
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = 'arial.ttf';
   $font_size = 30;
   imagettftext($image_p, $font_size, 0, ($width / 4), ($height - 30), $black, $font, $WaterMarkText);
   if ($DestinationFile<>'') {
      imagejpeg ($image_p, $DestinationFile, 100);
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
   };
   imagedestroy($image);
   imagedestroy($image_p);
};
addText ('images/'.$imagepath, 'test string', 'images/'.$imagepath);        
 
 
 
$sourceImage = 'images/'.$imagepath;
$watermarkImage = 'watermark.png';
$x = 0;
$y = 0;
 
 
$img = imagecreatefromjpeg($sourceImage);
imagesetbrush($img, imagecreatefrompng($watermarkImage));
imageline($img, $x, $y, $x, $y, IMG_COLOR_BRUSHED);
imagejpeg($img);
imagecopy('images/'.$imagepath, $img, 0, 0, 0, 0,0,0);
 
            echo "<img src='images/".$imagepath."'><br>"; 
        
          }
        }
?>
 
Its still not working. You can see how its working now at http://qualitycomputerri.com/pupeframes/
kagronick
Forum Newbie
Posts: 4
Joined: Thu Jul 30, 2009 12:30 am

Re: I can upload and resize image - I need to apply a PNG or GIF

Post by kagronick »

I finally got it working using some external classes. I would be more than happy to provide this PHP script that resizes, frames, and puts text on an image to anyone who wants it. (thats for the search engines) Email me at agronick (atsign) gmail.com
Post Reply