Jump to content

Recommended Posts

Hi guys ive been stuck all day since yesterday ive been trying figure out how to integrate water marking to existing upload class ive had no luck yet ive proberly tried 

about 30 water marking trying to integrate them but all failed

 

could anyone show me a way to intergrate water marking with this upload, im fairly new to php is still learning sorry if im seen noobish

 

 

<?php
/*
 ** This class contains the method to upload a file
 */
class Upload
{
    private $fileName; // name of the file from the upload form
    private $fileTypes = array(); // array of valid file types for upload
    private $folderPath; // folder where uploaded files will be moved
    public $msg; // messages generated from this class
    
    public function __construct($fileName, $fileTypes, $folderPath)
    {
        $this->fileName   = $fileName;
        $this->fileTypes  = $fileTypes;
        $this->folderPath = $folderPath;
    }
    
    public function isUploaded()
    // this function will contain the statements to process file upload
        
    // and returns the name of the file upon successful upload
        
    // or returns false otherwise.  It also sets the upload message ($this->msg)
    {
        $this->msg = '';
        // check if the name of the file uploaded is not available
        if (!$_FILES[$this->fileName]['name']) {
            $this->msg .= 'File name not available';
            return false;
        }
        // test for error in uploading
        if ($_FILES[$this->fileName]['error']) {
            switch ($_FILES[$this->fileName]['error']) {
                case 1:
                    $this->msg .= 'File exceeds PHP\'s maximum upload size<br />';
                    return false;
                case 2:
                    $this->msg .= 'File exceeds maximum upload file set in the form<br />';
                    return false;
                case 3:
                    $this->msg .= 'File partially uploaded<br />';
                    return false;
                case 4:
                    $this->msg .= 'No file uploaded<br />';
                    return false;
            }
        }
        // check if file type is invalid
        $type = $_FILES[$this->fileName]['type']; // get the type of the uploaded file
        if (!in_array($type, $this->fileTypes)) {
            $this->msg .= 'Wrong file type<br />';
            return false;
        }
        // check if file did not reach the server (temp location)
        if (!is_uploaded_file($_FILES[$this->fileName]['tmp_name'])) {
            $this->msg .= 'File did not reach temporary location on the server<br />';
            return false;
        }
        
        $fleName = $_FILES[$this->fileName]['name'];
        $flePath = $this->folderPath ? $this->folderPath . '/' . $fleName : $fleName;
        // test if a file with the same name exist, and rename if it does
        if (file_exists($flePath)) {
            
            
            
            
            
            $newName = uniqid('LD') . $fleName;
            $this->msg .= 'File ' . $fleName . ' already exists, renamed to ' . $newName . '<br />';
            $flePath = $this->folderPath ? $this->folderPath . '/' . $newName : $newName;
            
        }
        // move file from temporary location to the path specified 
        // and test if it's not successful
        if (!move_uploaded_file($_FILES[$this->fileName]['tmp_name'], $flePath)) {
            $this->msg .= 'Error in moving file to specified location<br />';
            return false;
        }
        // check if file does not exist on the destination folder
        if (!file_exists($flePath)) {
            $this->msg .= 'File did not reach destination folder<br />';
            return false;
        }
        
        $this->msg .= 'File ' . $_FILES[$this->fileName]['name'] . ' uploaded successfully<br />';
        return $flePath;
    }
}
?>
Edited by ignace
Added code tags

You need to show us what you tried and what didn't work.

 

Our rules specifically state that we are not here to do your homework. If you can't do the coursework, you deserve to fail. If you can make an effort and get help based on the work you've done, that's fine IMO.

hi ive tried a couple of them but i will load up some of the ones ive save as in water marking but i seems i not too sure how to intergrate a interchangeable one from the form to the ones that people had

made online >< im not sure how to fit the together into my upload class that does the watermarking.

 

 
<?php
 
//open up the images
$fleName = $_FILES[$this->fileName]['name'];
 
 
//get the image information
$dolpInfo = getimagesize('dolphin.jpg');
 
$watermark = imagecreatefrompng('optimized\images\watermark.png');
 
$waterInfo = getimagesize('optimized\images\watermark.png');
 
$x = $dolpInfo[0] - $waterInfo[0] - 5;
$y = $dolpInfo[1] - $waterInfo[1] - 5;
 
//add the watermark to the image
imagecopy($dolphin, $watermark, $x, $y, 0, 0, $waterInfo[0], $waterInfo[1]);
 
//display it onscreen
header('Content-Type: image/jpeg');
imagejpeg($dolphin);
 
 
?>
 

 

also tried intergrating 

 

<?php
// Load the stamp and the photo to apply the watermark to
$im = imagecreatefromjpeg('photo.jpeg');

// First we create our stamp image manually from GD
$stamp = imagecreatetruecolor(100, 70);
imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);
imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);
$im = imagecreatefromjpeg('photo.jpeg');
imagestring($stamp, 5, 20, 20, 'libGD', 0x0000FF);
imagestring($stamp, 3, 20, 40, '© 2007-9', 0x0000FF);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Merge the stamp onto our photo with an opacity of 50%
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);

// Save the image to file and free memory
imagepng($im, 'photo_stamp.png');
imagedestroy($im);

?>
 

and 

 

<?php
function watermarkImage ($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 = 10;
   imagettftext($image_p, $font_size, 0, 10, 20, $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);
};
?>
Edited by ignace
Added code tags

ive tried a few times but the quality of the teacher that thought me was very low it was his first class and he didnt make any sense and was sometimes teaching us backwards his grammar was horrible i did not understand anything or retrain anything he had said so i learn php my self slowly

It's amazing how many people come on here and complain about their teachers being horrible. Your response to a bad teacher is to basically give up? Don't schools these days have administration anymore? When I had a bad teacher I complained to the people who could actually do something about it.

 

Anyway since you've still not posted what errors you got with what code, you're still basically asking someone to do it for you.

 

In the future, use code tags when you post, your code is unreadable.

i did already complain to the head of department funny thing is she kept supporting him because she was the one who hired him.

ive been on trying to do water mark for almost 30hrs now and its here almost 3am almost im not trying to ask anyone to do it for me im at a point where i am stuck.

Again - what part are you "stuck" on? Do you understand exactly what you're trying to do? Once you know what your goal is, find the functions that will do it, then write the code. Stop trying to use other people's code, and write your own.

im not understanding how to put 

  1. if (!is_uploaded_file($_FILES[$this->fileName]['tmp_name'])) {
  2. $this->msg .= 'File did not reach temporary location on the server<br />';
  3. return false;
  4. }
  5.  
  6. $fleName = $_FILES[$this->fileName]['name'];
  7. $flePath = $this->folderPath ? $this->folderPath . '/' . $fleName : $fleName;
  8. // test if a file with the same name exist, and rename if it does
  9. if (file_exists($flePath)) {
  10.  
  11.  

as to  $fileName

 

 = $_FILES[$this->fileName]['name'];

 

 

 

 
$watermark = imagecreatefrompng('optimized\images\watermark.png');
 
$waterInfo = getimagesize('optimized\images\watermark.png');
 
$x = $filename[0] - $waterInfo[0] - 5;
$y = $filename[1] - $waterInfo[1] - 5;
 
//add the watermark to the image
imagecopy($filename, $watermark, $x, $y, 0, 0, $waterInfo[0], $waterInfo[1]);
 
//display it onscreen
header('Content-Type: image/jpeg');
imagejpeg($filename);
 
 

im not sure something like that but when i do i just get a blank page

basic method

 

  • create gd image from original photo using imagecreatefromjpeg
  • create second gd image from your watermark image
  • copy from watermark image to original using imagecopymerge so you can apply transparency
  • output original with imagejpeg(filename)
  • destroy both gd images

im not understanding how to put 

...

Your problem is that your trying to figure out how to combine pieces of pre-written PHP code. Instead you should just think about what process you have to go through to generate a watermarked image and then try and develop code for that. Once you know the proper process, figuring out where in your upload processing the code to do it should go should be relatively simple.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.