function wppa_use_thumb_file($id, $width = '0', $height = '0')
{
    if (!wppa_switch('use_thumbs_if_fit')) {
        return false;
    }
    if ($width <= 1.0 && $height <= 1.0) {
        return false;
    }
    // should give at least one dimension and not when fractional
    $file = wppa_get_thumb_path($id);
    if (file_exists($file)) {
        $size = wppa_get_imagexy($id, 'thumb');
    } else {
        return false;
    }
    if (!is_array($size)) {
        return false;
    }
    if ($width > 0 && $size[0] < $width) {
        return false;
    }
    if ($height > 0 && $size[1] < $height) {
        return false;
    }
    return true;
}
Example #2
0
function wppa_get_imgstyle_a($id, $file, $xmax_size, $xvalign = '', $type = '')
{
    global $wppa;
    $result = array('style' => '', 'width' => '', 'height' => '', 'cursor' => '', 'margin-top' => '', 'margin-bottom' => '');
    // Init
    wppa_cache_thumb($id);
    if (!$id) {
        return $result;
    }
    // no image: no dimensions
    if ($file == '') {
        return $result;
    }
    // no image: no dimensions
    if (wppa_has_audio($id)) {
        $file = wppa_fix_poster_ext($file, $id);
    }
    if (!wppa_is_video($id) && !is_file($file)) {
        wppa_dbg_msg('Please check file ' . $file . ' it is missing while expected. Id=' . $id, 'red');
        return $result;
        // no file: no dimensions ( 2.3.0 )
    }
    if (strpos($file, '/wppa/thumbs/')) {
        $image_attr = wppa_get_imagexy($id, 'thumb');
    } else {
        $image_attr = wppa_get_imagexy($id, 'photo');
    }
    //	if ( wppa_is_video( $id ) ) {
    //		$image_attr = array( '0' => wppa_get_videox( $id ), '1' => wppa_get_videoy( $id ) );
    //	}
    //	else {
    //		$image_attr = array( '0' => wppa_get_photox( $id ), '1' => wppa_get_photoy( $id ) );
    //	}
    if (!$image_attr || !isset($image_attr['0']) || !$image_attr['0'] || !isset($image_attr['1']) || !$image_attr['1']) {
        // File is corrupt
        wppa_dbg_msg('Please check file ' . $file . ' it is corrupted. If it is a thumbnail image,' . ' regenerate them using Table VIII-A4 of the Photo Albums -> Settings admin page.', 'red');
        return $result;
    }
    // Adjust for 'border'
    if ($type == 'fullsize' && !$wppa['in_widget']) {
        switch (wppa_opt('wppa_fullimage_border_width')) {
            case '':
                $max_size = $xmax_size;
                break;
            case '0':
                $max_size = $xmax_size - '2';
                break;
            default:
                $max_size = $xmax_size - '2' - 2 * wppa_opt('wppa_fullimage_border_width');
        }
    } else {
        $max_size = $xmax_size;
    }
    $ratioref = wppa_opt('wppa_maxheight') / wppa_opt('wppa_fullsize');
    $max_height = round($max_size * $ratioref);
    if ($type == 'fullsize') {
        if ($wppa['portrait_only']) {
            $width = $max_size;
            $height = round($width * $image_attr[1] / $image_attr[0]);
        } else {
            if (wppa_is_wider($image_attr[0], $image_attr[1])) {
                $width = $max_size;
                $height = round($width * $image_attr[1] / $image_attr[0]);
            } else {
                $height = round($ratioref * $max_size);
                $width = round($height * $image_attr[0] / $image_attr[1]);
            }
            if ($image_attr[0] < $width && $image_attr[1] < $height) {
                if (!$wppa['enlarge']) {
                    $width = $image_attr[0];
                    $height = $image_attr[1];
                }
            }
        }
    } else {
        if ($type == 'cover' && wppa_switch('wppa_coversize_is_height') && (wppa_opt('wppa_coverphoto_pos') == 'top' || wppa_opt('wppa_coverphoto_pos') == 'bottom')) {
            $height = $max_size;
            $width = round($max_size * $image_attr[0] / $image_attr[1]);
        } else {
            if (wppa_is_landscape($image_attr)) {
                $width = $max_size;
                $height = round($max_size * $image_attr[1] / $image_attr[0]);
            } else {
                $height = $max_size;
                $width = round($max_size * $image_attr[0] / $image_attr[1]);
            }
        }
    }
    switch ($type) {
        case 'cover':
            if (wppa_opt('wppa_bcolor_img') != '') {
                // There is a border color given
                $result['style'] .= ' border: 1px solid ' . wppa_opt('wppa_bcolor_img') . ';';
            } else {
                // No border color: no border
                $result['style'] .= ' border-width: 0px;';
            }
            $result['style'] .= ' width:' . $width . 'px; height:' . $height . 'px;';
            if (wppa_switch('wppa_use_cover_opacity') && !is_feed()) {
                $opac = wppa_opt('wppa_cover_opacity');
                $result['style'] .= ' opacity:' . $opac / 100 . '; filter:alpha( opacity=' . $opac . ' );';
            }
            if (wppa_opt('wppa_coverimg_linktype') == 'lightbox') {
                $result['cursor'] = ' cursor:url( ' . wppa_get_imgdir() . wppa_opt('wppa_magnifier') . ' ),pointer;';
            }
            break;
        case 'thumb':
            // Normal
        // Normal
        case 'ttthumb':
            // Topten
        // Topten
        case 'comthumb':
            // Comment widget
        // Comment widget
        case 'fthumb':
            // Filmthumb
        // Filmthumb
        case 'twthumb':
            // Thumbnail widget
        // Thumbnail widget
        case 'ltthumb':
            // Lasten widget
        // Lasten widget
        case 'albthumb':
            // Album widget
            if ($type == 'thumb' && wppa_get_get('hilite') == $id) {
                $result['style'] .= ' border:2px solid blue;box-sizing:border-box;';
            } else {
                $result['style'] .= ' border-width: 0px;';
            }
            $result['style'] .= ' width:' . $width . 'px; height:' . $height . 'px;';
            if ($xvalign == 'optional') {
                $valign = wppa_opt('wppa_valign');
            } else {
                $valign = $xvalign;
            }
            if ($valign != 'default') {
                // Center horizontally
                $delta = floor(($max_size - $width) / 2);
                if (is_numeric($valign)) {
                    $delta += $valign;
                }
                if ($delta < '0') {
                    $delta = '0';
                }
                if ($delta > '0') {
                    $result['style'] .= ' margin-left:' . $delta . 'px; margin-right:' . $delta . 'px;';
                }
            }
            switch ($valign) {
                case 'top':
                    $delta = $max_size - $height;
                    if ($delta < '0') {
                        $delta = '0';
                    }
                    $result['style'] .= ' margin-bottom: ' . $delta . 'px;';
                    $result['margin-bottom'] = $delta;
                    break;
                case 'center':
                    $delta = round(($max_size - $height) / 2);
                    if ($delta < '0') {
                        $delta = '0';
                    }
                    $result['style'] .= ' margin-top: ' . $delta . 'px; margin-bottom: ' . $delta . 'px;';
                    $result['margin-top'] = $delta;
                    $result['margin-bottom'] = $delta;
                    break;
                case 'bottom':
                    $delta = $max_size - $height;
                    if ($delta < '0') {
                        $delta = '0';
                    }
                    $result['style'] .= ' margin-top: ' . $delta . 'px;';
                    $result['margin-top'] = $delta;
                    break;
                default:
                    if (is_numeric($valign)) {
                        $delta = $valign;
                        $result['style'] .= ' margin-top: ' . $delta . 'px;';
                        $result['style'] .= ' margin-bottom: ' . $delta . 'px;';
                        $result['margin-top'] = $delta;
                        $result['margin-bottom'] = $delta;
                    }
            }
            if (wppa_switch('wppa_use_thumb_opacity') && !is_feed()) {
                $opac = wppa_opt('wppa_thumb_opacity');
                $result['style'] .= ' opacity:' . $opac / 100 . '; filter:alpha( opacity=' . $opac . ' );';
            }
            // Cursor
            $linktyp = '';
            switch ($type) {
                case 'thumb':
                    // Normal
                    $linktyp = wppa_opt('wppa_thumb_linktype');
                    break;
                case 'ttthumb':
                    // Topten	v
                    $linktyp = wppa_opt('wppa_topten_widget_linktype');
                    break;
                case 'comthumb':
                    // Comment widget	v
                    $linktyp = wppa_opt('wppa_comment_widget_linktype');
                    break;
                case 'fthumb':
                    // Filmthumb
                    $linktyp = wppa_opt('wppa_film_linktype');
                    break;
                case 'twthumb':
                    // Thumbnail widget	v
                    $linktyp = wppa_opt('wppa_thumbnail_widget_linktype');
                    break;
                case 'ltthumb':
                    // Lasten widget	v
                    $linktyp = wppa_opt('wppa_lasten_widget_linktype');
                    break;
                case 'albthumb':
                    // Album widget
                    $linktyp = wppa_opt('wppa_album_widget_linktype');
            }
            if ($linktyp == 'none') {
                $result['cursor'] = ' cursor:default;';
            } elseif ($linktyp == 'lightbox') {
                $result['cursor'] = ' cursor:url(' . wppa_get_imgdir() . wppa_opt('wppa_magnifier') . '),pointer;';
            } else {
                $result['cursor'] = ' cursor:pointer;';
            }
            break;
        case 'fullsize':
            if ($wppa['auto_colwidth']) {
                // These sizes fit within the rectangle define by Table I-B1,2
                // times 2 for responsive themes,
                // and are supplied for ver 4 browsers as they have undefined natural sizes.
                $result['style'] .= ' max-width:' . $width * 2 . 'px;';
                $result['style'] .= ' max-height:' . $height * 2 . 'px;';
            } else {
                // These sizes fit within the rectangle define by Table I-B1,2
                // and are supplied for ver 4 browsers as they have undefined natural sizes.
                $result['style'] .= ' max-width:' . $width . 'px;';
                $result['style'] .= ' max-height:' . $height . 'px;';
                $result['style'] .= ' width:' . $width . 'px;';
                $result['style'] .= ' height:' . $height . 'px;';
            }
            if ($wppa['is_slideonly'] == '1') {
                if ($wppa['ss_widget_valign'] != '') {
                    $valign = $wppa['ss_widget_valign'];
                } else {
                    $valign = 'fit';
                }
            } elseif ($xvalign == 'optional') {
                $valign = wppa_opt('wppa_fullvalign');
            } else {
                $valign = $xvalign;
            }
            // Margin
            if ($valign != 'default') {
                $m_left = '0';
                $m_right = '0';
                $m_top = '0';
                $m_bottom = '0';
                // Center horizontally
                $delta = round(($max_size - $width) / 2);
                if ($delta < '0') {
                    $delta = '0';
                }
                if ($wppa['auto_colwidth']) {
                    $m_left = 'auto';
                    $m_right = 'auto';
                } else {
                    $m_left = $delta;
                    $m_right = '0';
                }
                // Position vertically
                if ($wppa['in_widget'] == 'ss' && $wppa['in_widget_frame_height'] > '0') {
                    $max_height = $wppa['in_widget_frame_height'];
                }
                $delta = '0';
                if (!$wppa['auto_colwidth'] && !wppa_page('oneofone')) {
                    switch ($valign) {
                        case 'top':
                        case 'fit':
                            $delta = '0';
                            break;
                        case 'center':
                            $delta = round(($max_height - $height) / 2);
                            if ($delta < '0') {
                                $delta = '0';
                            }
                            break;
                        case 'bottom':
                            $delta = $max_height - $height;
                            if ($delta < '0') {
                                $delta = '0';
                            }
                            break;
                    }
                }
                $m_top = $delta;
                $result['style'] .= wppa_combine_style('margin', $m_top, $m_left, $m_right, $m_bottom);
            }
            // Border and padding
            if (!$wppa['in_widget']) {
                switch (wppa_opt('wppa_fullimage_border_width')) {
                    case '':
                        break;
                    case '0':
                        $result['style'] .= ' border: 1px solid ' . wppa_opt('wppa_bcolor_fullimg') . ';';
                        break;
                    default:
                        $result['style'] .= ' border: 1px solid ' . wppa_opt('wppa_bcolor_fullimg') . ';';
                        $result['style'] .= ' background-color:' . wppa_opt('wppa_bgcolor_fullimg') . ';';
                        $result['style'] .= ' padding:' . wppa_opt('wppa_fullimage_border_width') . 'px;';
                        // If we do round corners...
                        if (wppa_opt('wppa_bradius') > '0') {
                            // then also here
                            $result['style'] .= ' border-radius:' . wppa_opt('wppa_fullimage_border_width') . 'px;';
                        }
                }
            }
            break;
        default:
            $wppa['out'] .= 'Error wrong "$type" argument: ' . $type . ' in wppa_get_imgstyle_a';
    }
    $result['width'] = $width;
    $result['height'] = $height;
    return $result;
}
function wppa_create_textual_watermark_file($args)
{
    // See what we have
    $args = wp_parse_args((array) $args, array('content' => '---preview---', 'pos' => 'cencen', 'id' => '', 'font' => wppa_opt('wppa_textual_watermark_font'), 'text' => '', 'style' => wppa_opt('wppa_textual_watermark_type'), 'filebasename' => 'dummy', 'url' => false, 'width' => '', 'height' => '', 'transp' => '0'));
    // We may have been called from wppa_get_water_file_and_pos() just to find the settings
    // In this case there is no id given.
    $id = $args['id'];
    if (!$id && $args['content'] != '---preview---') {
        return false;
    }
    if ($id && wppa_is_video($id)) {
        //		return false;
    }
    // Set special values in case of preview
    if ($args['content'] == '---preview---') {
        $preview = true;
        $fontsize = $args['font'] == 'system' ? 5 : 12;
        $padding = 6;
        $linespacing = ceil($fontsize * 2 / 3);
    } else {
        $preview = false;
        $fontsize = wppa_opt('wppa_textual_watermark_size');
        if ($args['font'] == 'system') {
            $fontsize = min($fontsize, 5);
        }
        $padding = 12;
        $linespacing = ceil($fontsize * 2 / 3);
    }
    // Set font specific vars
    $fontfile = $args['font'] == 'system' ? '' : WPPA_UPLOAD_PATH . '/fonts/' . $args['font'] . '.ttf';
    // Output file
    if ($preview) {
        $filename = WPPA_UPLOAD_PATH . '/fonts/wmf' . $args['filebasename'] . '.png';
    } else {
        $filename = WPPA_UPLOAD_PATH . '/temp/wmf' . $args['filebasename'] . '.png';
    }
    // Preprocess the text
    if (!$args['text']) {
        switch ($args['content']) {
            case '---preview---':
                $text = strtoupper(substr($args['font'], 0, 1)) . strtolower(substr($args['font'], 1));
                break;
            case '---filename---':
                $text = wppa_get_photo_item($id, 'filename');
                break;
            case '---name---':
                $text = wppa_get_photo_name($id);
                break;
            case '---description---':
                $text = strip_tags(wppa_strip_tags(wppa_get_photo_desc($id), 'style&script'));
                break;
            case '---predef---':
                $text = wppa_opt('wppa_textual_watermark_text');
                if ($args['font'] != 'system') {
                    $text = str_replace('( c )', '&copy;', $text);
                    $text = str_replace('( R )', '&reg;', $text);
                }
                $text = html_entity_decode($text);
                $text = str_replace('w#site', get_bloginfo('url'), $text);
                $text = str_replace('w#owner', wppa_get_photo_item($id, 'owner'), $text);
                $text = str_replace('w#id', $id, $text);
                $text = str_replace('w#name', wppa_get_photo_name($id), $text);
                $text = str_replace('w#filename', wppa_get_photo_item($id, 'filename'), $text);
                $text = str_replace('w#timestamp', wppa_local_date(get_option('date_format', "F j, Y,") . ' ' . get_option('time_format', "g:i a"), wppa_get_photo_item($id, 'timestamp')), $text);
                $text = trim($text);
                break;
            default:
                wppa_log('Error', 'Unimplemented arg ' . $arg . ' in wppa_create_textual_watermark_file()');
                return false;
        }
    } else {
        $text = $args['text'];
    }
    // Any text anyway?
    if (!strlen($text)) {
        wppa_log('Error', 'No text for textual watermark. photo=' . $id);
        return false;
        // No text -> no watermark
    }
    // Split text on linebreaks
    $text = str_replace("\n", '\\n', $text);
    $lines = explode('\\n', $text);
    // Trim and remove empty lines
    $temp = $lines;
    $lines = array();
    foreach ($temp as $line) {
        $line = trim($line);
        if ($line) {
            $lines[] = $line;
        }
    }
    // Find image width
    if ($args['width']) {
        $image_width = $args['width'];
    } else {
        $image_width = '';
    }
    if ($args['height']) {
        $image_height = $args['height'];
    } else {
        $image_height = '';
    }
    if ($preview) {
        if (!$image_width) {
            $image_width = 2000;
        }
        if (!$image_height) {
            $image_height = 1000;
        }
    } else {
        //		$temp = getimagesize( wppa_get_photo_path( $id ) );
        $temp = wppa_get_imagexy($id);
        if (!is_array($temp)) {
            wppa_log('Error', 'Trying to apply a watermark on a non image file. Id = ' . $id);
            return false;
            // not an image
        }
        if (!$image_width) {
            $image_width = $temp[0];
        }
        if (!$image_height) {
            $image_height = $temp[1];
        }
    }
    $width_fits = false;
    while (!$width_fits) {
        // Find pixel linelengths
        foreach (array_keys($lines) as $key) {
            $lines[$key] = trim($lines[$key]);
            if ($args['font'] == 'system') {
                $lengths[$key] = strlen($lines[$key]) * imagefontwidth($fontsize);
            } else {
                $temp = imagettfbbox($fontsize, 0.0, $fontfile, $lines[$key]);
                $lengths[$key] = $temp[2] - $temp[0];
            }
        }
        $maxlen = wppa_array_max($lengths);
        // Find canvas size
        $nlines = count($lines);
        if ($args['font'] == 'system') {
            $lineheight = imagefontheight($fontsize);
        } else {
            $temp = imagettfbbox($fontsize, 0.0, $fontfile, $lines[0]);
            $lineheight = $temp[3] - $temp[7];
        }
        $canvas_width = wppa_array_max($lengths) + 4 * $padding;
        $canvas_height = ($lineheight + $linespacing) * count($lines) + 2 * $padding;
        // Does it fit?
        if ($canvas_width > $image_width) {
            // Break the longest line into two sublines. There should be a space in the right half, if not: fail
            $i = 0;
            $l = 0;
            foreach (array_keys($lines) as $key) {
                if (strlen($lines[$key]) > $l) {
                    $i = $key;
                    $l = strlen($lines[$key]);
                }
            }
            $temp = $lines;
            $lines = array();
            $j = 0;
            while ($j < $i) {
                $lines[$j] = $temp[$j];
                $j++;
            }
            //
            $j = $i;
            $llen = strlen($temp[$i]);
            $spos = floor($llen / 2);
            while ($spos < $llen && substr($temp[$i], $spos, 1) != ' ') {
                $spos++;
            }
            if ($spos == $llen) {
                // Unable to find a space, give up
                wppa_log('Error', 'Trying to apply a watermark that is too wide for the image. Id = ' . $id);
                return false;
                // too wide
            }
            $lines[$j] = substr($temp[$i], 0, $spos);
            $lines[$j + 1] = trim(str_replace($lines[$j], '', $temp[$i]));
            $i++;
            //
            $j = $i + 1;
            while ($j <= count($temp)) {
                $lines[$j] = $temp[$i];
                $j++;
            }
        } else {
            $width_fits = true;
        }
        if ($canvas_height > $image_height) {
            wppa_log('Error', 'Trying to apply a watermark that is too high for the image. Id = ' . $id);
            return false;
            // not an image
        }
    }
    // Create canvas
    $canvas = imagecreatetruecolor($canvas_width, $canvas_height);
    $bgcolor = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
    // Transparent
    $white = imagecolorallocatealpha($canvas, 255, 255, 255, $args['transp']);
    $black = imagecolorallocatealpha($canvas, 0, 0, 0, $args['transp']);
    imagefill($canvas, 0, 0, $bgcolor);
    //	imagerectangle( $canvas, 0, 0, $canvas_width-1, $canvas_height-1, $white );	// debug
    // Define the text colors
    switch ($args['style']) {
        case 'tvstyle':
        case 'whiteonblack':
            $fg = $white;
            $bg = $black;
            break;
        case 'utopia':
        case 'blackonwhite':
            $fg = $black;
            $bg = $white;
            break;
        case 'white':
            $fg = $white;
            $bg = $bgcolor;
            break;
        case 'black':
            $fg = $black;
            $bg = $bgcolor;
            break;
    }
    // Plot the text
    foreach (array_keys($lines) as $lineno) {
        if (strpos($args['pos'], 'lft') !== false) {
            $indent = 0;
        } elseif (strpos($args['pos'], 'rht') !== false) {
            $indent = $maxlen - $lengths[$lineno];
        } else {
            $indent = floor(($maxlen - $lengths[$lineno]) / 2);
        }
        switch ($args['style']) {
            case 'tvstyle':
            case 'utopia':
                for ($i = -1; $i <= 1; $i++) {
                    for ($j = -1; $j <= 1; $j++) {
                        if ($args['font'] == 'system') {
                            imagestring($canvas, $fontsize, 2 * $padding + $i + $indent, $padding + $lineno * ($lineheight + $linespacing) + $j, $lines[$lineno], $bg);
                        } else {
                            imagettftext($canvas, $fontsize, 0, 2 * $padding + $i + $indent, $padding + ($lineno + 1) * $lineheight + $lineno * $linespacing + $j, $bg, $fontfile, $lines[$lineno]);
                        }
                    }
                }
                if ($args['font'] == 'system') {
                    imagestring($canvas, $fontsize, 2 * $padding + $indent, $padding + $lineno * ($lineheight + $linespacing), $lines[$lineno], $fg);
                } else {
                    imagettftext($canvas, $fontsize, 0, 2 * $padding + $indent, $padding + ($lineno + 1) * $lineheight + $lineno * $linespacing, $fg, $fontfile, $lines[$lineno]);
                }
                break;
            case 'blackonwhite':
            case 'whiteonblack':
            case 'white':
            case 'black':
                $lft = $padding + $indent;
                $rht = 3 * $padding + $indent + $lengths[$lineno];
                $top = $lineno * ($lineheight + $linespacing) + $padding;
                $bot = ($lineno + 1) * ($lineheight + $linespacing) + $padding;
                imagefilledrectangle($canvas, $lft, $top + 1, $rht, $bot, $bg);
                //				imagerectangle( $canvas, $lft, $top, $rht, $bot, $fg );	// debug
                $top = $padding + $lineno * ($lineheight + $linespacing) + floor($linespacing / 2);
                $lft = 2 * $padding + $indent;
                $bot = $padding + ($lineno + 1) * ($lineheight + $linespacing) - ceil($linespacing / 2);
                if ($args['font'] == 'system') {
                    imagestring($canvas, $fontsize, $lft, $top, $lines[$lineno], $fg);
                } else {
                    imagettftext($canvas, $fontsize, 0, $lft, $bot - 1, $fg, $fontfile, $lines[$lineno]);
                }
                break;
        }
    }
    imagesavealpha($canvas, true);
    imagepng($canvas, $filename);
    imagedestroy($canvas);
    if ($preview || $args['url']) {
        $url = str_replace(WPPA_UPLOAD_PATH, WPPA_UPLOAD_URL, $filename);
        return $url;
    } else {
        return $filename;
    }
}