Page 1 of 1

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

Posted: Thu Jul 30, 2009 12:33 am
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>"; 
        
          }
        }
?>

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

Posted: Thu Jul 30, 2009 1:13 am
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.

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

Posted: Thu Jul 30, 2009 1:44 am
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>"; 
        
          }
        }
?>

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

Posted: Thu Jul 30, 2009 9:11 am
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.

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

Posted: Thu Jul 30, 2009 9:53 am
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).

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

Posted: Thu Jul 30, 2009 6:23 pm
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/

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

Posted: Thu Jul 30, 2009 7:01 pm
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