resize watermark image

Ask about a PHP problem here.
Post Reply
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

resize watermark image

Post by Robbedoesie »

Hello,
I have an uploading image with watermark script. I also want that to resize the images. I have resize script but with al my efforts to watermark and resize the images while uploading it or watermarks the images or resize the images, never watermark and resize the images. So I put the resize and watermark functions together in one function but I'm not get this to work. It keeps the thumbnails black and when I click on the thumbnail it only shows the watermark, and also that is black.
What can I do to make this work?
The resize and watermark function is;
[syntax=php]function watermark_image($target, $output, $w, $h, $ext, $wm_image) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);

imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}

$info = getimagesize($wm_image);

switch ($info['mime']){
case 'image/jpeg';
$main = imagecreatefromjpeg($wm_image);
break;
case 'image/png';
$main = imagecreatefrompng($wm_image);
break;
case 'image/gif';
$main = imagecreatefromgif($wm_image);
break;
default:
return false;
}
imagealphablending($main, true);

imagecopy($tci, $main, 5, 5, 0, 0, imagesx($overlay), imagesy($overlay));
imagepng($main, $output);
}[/syntax]
Thanks
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: resize watermark image

Post by jacek »

Do you understand the code ? It seems like there are a lot of problems coming from things like incorrect use of functions, it might be easier to go back to something that was working and try to understand it better then from there create your own code that does what you want.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: resize watermark image

Post by Robbedoesie »

He Jacek,
so I made a mess from this? Hmm, not good. I am trying to make this work.

I have to good working functions. One watermark image function from you and one resize image function;
[syntax=php]function img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);

imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
[/syntax]
And I know what everything is doing, everything is well explained in tutorials and php.net.
What I really don't know is how to combine these watermark and resize functions together inside one function. There is not much to find on the internet about this. There are a few tutorials but it is in php oop and I am not good at all in php oop.

I gonna do my best but I appreciate all the help I get.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: resize watermark image

Post by Temor »

It looks like there might be a few other ways to do that more efficiently.

$newcopy never gets a value assigned to it. At least not first function you posted. Start there.
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: resize watermark image

Post by Robbedoesie »

Oke, this far to difficult for me at this moment. I have to learn more about php and I have to learn more about combine functions. I thought this wouldn't be that difficult but it is.
Is there somewhere tutorials about combine functions? I can't find any.
Thanks
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: resize watermark image

Post by jacek »

Robbedoesie wrote:Is there somewhere tutorials about combine functions? I can't find any.

Sounds like you are searching for the wrong thing, instead of trying to combine what you have to make it work try to come up with something new that does what you want.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: resize watermark image

Post by Robbedoesie »

I couldn't make the watermark and resize image function working separately but together in one function it all works good :D .
This is the code;
[syntax=php]function watermark_resize_function($source_image,$destination,$tn_w,$tn_h,$quality = 80,$wmsource) {
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);

switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}

$src_w = imagesx($source);
$src_h = imagesy($source);
$src_ratio = $src_w/$src_h;
if ($tn_w/$tn_h > $src_ratio) {
$tn_w = $tn_h * $src_ratio;
} else {
$tn_h = $tn_w / $src_ratio;
}

$newpic = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $tn_w, $tn_h, $src_w, $src_h);

if($wmsource) {
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);

switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
return false;
}

imagealphablending($newpic, true);
$img_w = imagesx($newpic);
$img_h = imagesy($newpic);
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
$dst_x = ($img_w / 2) - ($wm_w / 2); // For centering the watermark on any image
$dst_y = ($img_h / 2) - ($wm_h / 2); // For centering the watermark on any image

imagecopy($newpic, $watermark, $dst_x, $dst_y, 0, 0, $wm_w, $wm_h);
}
if(Imagejpeg($newpic,$destination,$quality)) {
return true;
}
return false;
}[/syntax]
Post Reply