Hi i wrote a code for image upload and watermarking. It is working fine in Jpg but not in gif.... The code is

$ext = end(explode('.', $final_file));
if($ext == 'jpg'){
$ext_new = 'jpeg';
} elseif ($ext == 'JPG' || $ext == 'JPEG'){
$ext_new = 'jpeg';
}else {
$ext_new = $ext;
}

$po="imagecreatefrom".$ext_new;
//////////////////Resize///////////////

$finalfilename="share/".$final_file;
$src = $po($finalfilename);
list($width,$height)=getimagesize($finalfilename);
$newwidth=200;
$newheight=200;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "share/".$final_file;
imagejpg($tmp,$filename,100);
imagedestroy($src);
//unlink($final_file);
imagedestroy($tmp);
////////////////watermark///////////////////////////
$orgimage=$po($finalfilename); // your image
$watermark=imagecreatefrompng("logo.png");  //transparent watermark logo
// $watermark=imagecreatefromgif("yourlogo.gif");  
imagecopy($orgimage,$watermark,0,0,0,0,imagesx($watermark),imagesy($watermark));

imagejpeg($orgimage,$filename,100);
imagedestroy($watermark);
imagedestroy($orgimage);

The following error :

Warning: imagecreatefromgif() [function.imagecreatefromgif]: 'share/-yyyy-.gif' is not a valid GIF file in C:\wamp\www\water\process.php on line 98

Warning: imagecopy(): supplied argument is not a valid Image resource in C:\wamp\www\water\process.php on line 101

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\water\process.php on line 103

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\water\process.php on line 105

Please help me with the correction

Not sure how you made it work even with a JPEG. I found a number of errors:
1. In one case you have used imagejpg instead of imagejpeg
2. You have a statement:

$src = $po($finalfilename);

which makes no sense to me. It seems that the source file should be referenced as $final_file:

$src = $po($final_file);

I had the original image in the same directory as the php module with the resulting file in a sub-directory called "share". That was how I interpreted what you did.

With those changes, it worked for me for a jpeg.

To make it work with a .gif, you need to get rid of the hard-coding for jpeg. I added a staement:

$c_create = "image".$ext_new;

and then I used $c_create in place of your imagejpeg as in:

$c_create($orgimage,$filename,100);

Having done all that, it worked for both jpeg and gif (and should work with anything else that PHP supports).

There are a few logical errors in your code like (chrishea) stated and got it working.
If it solved now mark as solved else Ive noted maybe a few directory problems but depends how you set it up.
Note: Ill comment where I think you should go try and debug

// Orginal file is $final_file
$ext = end(explode('.', $final_file));
if($ext == 'jpg'){
$ext_new = 'jpeg';
} elseif ($ext == 'JPG' || $ext == 'JPEG'){
$ext_new = 'jpeg';
}else {
$ext_new = $ext;
}

$po="imagecreatefrom".$ext_new;
//////////////////Resize///////////////
// Im abit confused but the directories look wrong/confusing
// $po, $finalfilename, $src
$finalfilename="share/".$final_file;
$src = $po($finalfilename);
list($width,$height)=getimagesize($finalfilename);
$newwidth=200;
$newheight=200;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "share/".$final_file;
imagejpg($tmp,$filename,100);
imagedestroy($src);
//unlink($final_file);
imagedestroy($tmp);
////////////////watermark///////////////////////////
$orgimage=$po($finalfilename); // your image
$watermark=imagecreatefrompng("logo.png");  //transparent watermark logo
// $watermark=imagecreatefromgif("yourlogo.gif");  
imagecopy($orgimage,$watermark,0,0,0,0,imagesx($watermark),imagesy($watermark));

imagejpeg($orgimage,$filename,100);
imagedestroy($watermark);
imagedestroy($orgimage);$ext = end(explode('.', $final_file));

I will a closer look later if time allows check that and get back to me.

Please help me ... GIF doesnt working. JPEG Working well

In my opinion the first 9 lines are written poorly and should be replaced with the following:

// Orginal file is $final_file
$ext = end(explode('.', $final_file));
if(strtolower($ext) == 'jpg' || strtolower($ext) == 'jpeg'){
$ext_new = 'jpeg';
} elseif (strtolower($ext) == 'gif'){
$ext_new = 'gif';
} else {
die ('Invalid Extension.');
}

Perhaps it was the casing of the gif extension that messed it up.

yes, its good suggestion. try the strtolower case

but not working

Just for an update, what error messages are you getting for the most recent code and what is the most recent code your using?

working till the upload process. And no watermarking for gif file. No problem with JPEG/JPG
Thanks

code:

$ext = end(explode('.', $final_file));
if(strtolower($ext) == 'jpg' || strtolower($ext) == 'jpeg'){
$ext_new = 'jpeg';
} elseif (strtolower($ext) == 'gif'){
$ext_new = 'gif';
} else {
die ('Invalid Extension.');
}
$po="imagecreatefrom".$ext_new;
//////////////////Resize///////////////

$finalfilename="share/".$final_file;
$src = $po($finalfilename);
list($width,$height)=getimagesize($finalfilename);
$newwidth=200;
$newheight=200;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "share/".$final_file;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
//unlink($final_file);
imagedestroy($tmp);



$orgimage=@$po("$finalfilename"); // your image
$watermark=@imagecreatefrompng("logo.png");  //transparent watermark logo
// $watermark=imagecreatefromgif("yourlogo.gif");  
@imagecopy($orgimage,$watermark,0,0,0,0,imagesx($watermark),imagesy($watermark));

$c_create = "image".$ext_new;

@$c_create($orgimage,$filename,100);
@imagedestroy($watermark);
@imagedestroy($orgimage);

Perhaps this code will help. I noticed that the imagecreatefromgif was trying to open a jpeg file.

$ext = end(explode('.', $final_file));
if(strtolower($ext) == 'jpg' || strtolower($ext) == 'jpeg'){
    $ext_new = 'jpeg';
    $imgsave='imagejpg';
    } elseif (strtolower($ext) == 'gif'){
    $ext_new = 'gif';
    $imgsave='imagegif';
    } else {
    die ('Invalid Extension.');
    }

$po="imagecreatefrom".$ext_new;
//////////////////Resize///////////////
// Im abit confused but the directories look wrong/confusing
// $po, $finalfilename, $src
$finalfilename="share/".$final_file;
$src = $po($finalfilename);
list($width,$height)=getimagesize($finalfilename);
$newwidth=200;
$newheight=200;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "share/".$final_file;
if ($imagesave=='imagejpg') {
    $imagesave($tmp,$filename,100);
    } else {
    $imagesave($tmp,$filename);
    }
imagedestroy($src);
//unlink($final_file);
imagedestroy($tmp);
////////////////watermark///////////////////////////
$orgimage=$po($finalfilename); // your image
$watermark=imagecreatefrompng("logo.png");  //transparent watermark logo
// $watermark=imagecreatefromgif("yourlogo.gif");  
imagecopy($orgimage,$watermark,0,0,0,0,imagesx($watermark),imagesy($watermark));

imagejpeg($orgimage,$filename,100);
imagedestroy($watermark);
imagedestroy($orgimage);$ext = end(explode('.', $final_file));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.