Page 1 of 1

resize watermark image

Posted: Mon Oct 15, 2012 8:53 pm
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;
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);
	}
Thanks

Re: resize watermark image

Posted: Thu Oct 18, 2012 5:54 pm
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.

Re: resize watermark image

Posted: Fri Oct 19, 2012 7:48 am
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;
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);
    }
}
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.

Re: resize watermark image

Posted: Sun Oct 21, 2012 6:50 pm
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.

Re: resize watermark image

Posted: Sun Oct 21, 2012 9:41 pm
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

Re: resize watermark image

Posted: Sat Oct 27, 2012 9:45 pm
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.

Re: resize watermark image

Posted: Tue Oct 30, 2012 9:53 pm
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;
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;
}