Jump to content

Help With Images


moon 111

Recommended Posts

I have this function:

[code]
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile, $x, $y, $font_size) {
  if(stripos($SourceFile, ".png"))
  {
    $ext = ".png";
  }
  elseif(stripos($SourceFile, ".jpg") OR stripos($SourceFile, ".jpeg"))
  {
    $ext = ".jpg";
  }
  
  list($width, $height) = getimagesize($SourceFile);
  $image_p = imagecreatetruecolor($width, $height);
  if($ext == ".png") $image = imagecreatefrompng($SourceFile);
  elseif($ext == ".jpg") $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';
  imagettftext($image_p, $font_size, 0, $x, $y, $black, $font, $WaterMarkText);
  if ($DestinationFile<>'') {
    if($ext == ".png") imagepng ($image_p, $DestinationFile, 100);
    elseif($ext == ".jpg") imagejpeg ($image_p, $DestinationFile, 100);
  } else {
    if($ext == ".png")
    {
      header('Content-Type: image/png');
      imagepng($image_p, null, 100);
    }
    elseif($ext == ".jpg")
    {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
    }
  }
  imagedestroy($image);
  imagedestroy($image_p);
}

?>

 

But it only works on .jpegs. Want I want to do is make it work with pngs too. I tried this but it didn't work:

 

<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile, $x, $y, $font_size) {
  if(stripos($SourceFile, ".png"))
  {
    $ext = ".png";
  }
  elseif(stripos($SourceFile, ".jpg") OR stripos($SourceFile, ".jpeg"))
  {
    $ext = ".jpg";
  }
  
  list($width, $height) = getimagesize($SourceFile);
  $image_p = imagecreatetruecolor($width, $height);
  if($ext == ".png") $image = imagecreatefrompng($SourceFile);
  elseif($ext == ".jpg") $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';
  imagettftext($image_p, $font_size, 0, $x, $y, $black, $font, $WaterMarkText);
  if ($DestinationFile<>'') {
    if($ext == ".png") imagepng ($image_p, $DestinationFile, 100);
    elseif($ext == ".jpg") imagejpeg ($image_p, $DestinationFile, 100);
  } else {
    if($ext == ".png")
    {
      header('Content-Type: image/png');
      imagepng($image_p, null, 100);
    }
    elseif($ext == ".jpg")
    {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
    }
  }
  imagedestroy($image);
  imagedestroy($image_p);
}

?>

 

Help please[/code]

Link to comment
https://forums.phpfreaks.com/topic/97783-help-with-images/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.