0

I´m using this function to add a watermark on the top left corner of an image, jpeg or png.

I need the Switch and Case to differentiate between the png and the jpeg to process each image type separately.

Each time I run the function, images as edited perfectly, but nevertheless the debug shows that the images are being processed both as .png and .jpg therefore emitting a problem when the image is of the wrong type.

function addWatermark($path)
{
    $public_file_path = dirname(__FILE__);
    $font = $public_file_path . '/src/fonts/Roboto-Light.ttf'; 
    // get fiel type for switch
    $size = getimagesize($path);
    // get $from name
    $path_parts = pathinfo($path); 
    $text= $path_parts['filename'];
    switch($path_parts['extension'])
    {
        case "jpeg" or "jpg":
            // Copy and resample the imag
            list($width, $height) = getimagesize($path);
            $image_p = imagecreatefromjpeg($path);
            // Prepare font size and colors
            $text_color = imagecolorallocate($image_p, 0, 0, 0);
            $bg_color = imagecolorallocate($image_p, 255, 255, 255);
            $font_size = 17; 
            // Set the offset x and y for the text position
            $offset_x = 0;
            $offset_y = 20;
            // Get the size of the text area
            $dims = imagettfbbox($font_size, 0, $font, $text);
            $text_width = $dims[4] - $dims[6] + $offset_x;
            $text_height = $dims[3] - $dims[5] + $offset_y;
            // Add text background
            imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);
            // Add text
            imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
            // Save the picture
            imagejpeg($image_p, $path, 90); 
            // Clear
            imagedestroy($image_p);
        case "png":
            $im = imagecreatefrompng($path);
            imagesavealpha($im, true); // important to keep the png's transparency 
            $text_color = imagecolorallocate($im, 0, 0, 0);
            $width = imagesx($im); // the width of the image
            $height = imagesy($im);; // the heighst of the image
            $font_size = 15; // font size
            $box_color = imagecolorallocate($im, 255, 255, 255);
            // Set the offset x and y for the text position
            $offset_x = 0;
            $offset_y = 20;
            $dims = imagettfbbox($font_size, 0, $font, $text);
            $text_width = $dims[4] - $dims[6] + $offset_x;
            $text_height = $dims[3] - $dims[5] + $offset_y;
            // Add text background
            imagefilledrectangle($im, 0, 0, $text_width, $text_height, $box_color);
            // Add text
            imagettftext($im, $font_size, 0, $offset_x, $offset_y, $text_color, $font,$text);
            imagepng($im, $path, 0);
            imagedestroy($im);
        case "mp4";
            break;
    }
    // hasta aca
}

3 Answers3

4

I don't believe you can use an or in the case statement. From this example, everything will match the first case: http://sandbox.onlinephpfunctions.com/code/8edfd01d386e9f7bb56975b9b1ecb9dee4669494

Your code should be:

switch($path_parts['extension'])
{
    case "jpeg":
    case "jpg":
        // Your code
        break;
    case "png":
        // Your code
        break;
    case "mp4";
        break;
}
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
1

You will need to add break; to your case statements.

switch($path_parts['extension'])
{
    case "jpeg":
    case "jpg":
        // Your code
        break;
    case "png":
        // Your code
        break;
    case "mp4";
        break;
}
GrumpyToaster
  • 335
  • 2
  • 12
0

So it looks like you may be missing break statments and I'm not sure if case('jpg') or case('jpeg') will work. You can use this alternative:

switch(var) {
    case('jpg'):
    case('jpeg'):
break;

case('png'):
break;

default:
break;
}

Erik Flitman
  • 163
  • 4