<?php
/*		Freewheeling Easy Mapping Application
 *		A collection of routines for display of trail maps and amenities
 *		copyright Roy R Weil 2019 - https://royweil.com
 *
 */
class AppendPhotographer
{
    private static function makeImages($sourceFile, $photographer, $width, $widthThumb, $debug = false)
    {
        /*
        Assume that the max height limit is handler by the caller
        create two file from the original
            1)  a) resize to $width
                b) append bottom botder with "photo by photragrapher"
                c) add _cr to filename and place in same direcory
            2)  a) resize to $widthThumb
                b) add _tmb to filename and plave in same directory
        $debug if odd trace file nameing comvensins
        $debug if mod 2= 1 trace image creation
        Throw errors for any problems
        Return any messages
        does not change the exif data.
        */
        $eol = "<br />";
        $errorBeg = "$eol<style 'text-color:red' >";
        $errorEnd = "</style> $eol";
        global $photoUrl, $photoPath, $thumbUrl, $thumbPath, $highresUrl, $highresPath;
        //      creates the _cr version with bottom line credit of photographer
        //      creates the thumbnail version
        //      moves to the high_resoltion directory
        $msg = "";
        try {
            if (false === $debug)
                $debug = rrwUtil::setDebug("makeimage");
            if (false !== $debug) {
                $debugImageWork = $debug | 2;
                $debug = $debug | 1;
            }
            if ($debug) $msg = "makeImages( $sourceFile, $photographer ) $eol";
            $h_botWhite = 20; #	height of the white bar at the bottom for copyright notice
            $fontSize = 12; #	height of the copyright text
            $fontfile = "arial.ttf";
            $fontDire = "/home/pillowan/www-shaw-weil-pictures/wp-content/plugins/roys-picture-processing";
            $fontfile = "$fontDire/mvboli.ttf";
            $iiSlash = strrpos($sourceFile, "/");
            $direSource = substr($sourceFile, $iiSlash);
            $fullfileThumb = "$direSource" . "_tmb.jpg";
            $fullFilePhoto = "$direSource" . "_cr.jpg";
            if ($debug) {
                $msg .= "direSource = $direSource $eol";
                $msg .= "fullfileThumb : $fullfileThumb $eol";
                $msg .= "fullFilePhoto : $fullFilePhoto $eol";
            }
            //  ------------------------------------- got the file now process it
            if (!file_exists($fontfile)) {
                $msg .= "bad font $fontfile ";
                throw new Exception("$msg $errorBeg E#132 Problems with the font file $errorEnd");
            }
            // create new dimensions, keeping aspect ratio
            $imageInfo = getimagesize($sourceFile);
            $w_src = $imageInfo[0];
            $h_src = $imageInfo[1];
            if ($h_src == 0 || $w_src == 0) {
                throw new Exception(" $errorBeg width is $w_src or Height is $h_src zero $errorEnd");
            }
            //  ------------------------------------------- resize thumbnail
            $aspect = $h_src / $w_src; // maintain the aspect ration
            $w_tmb = $widthThumb;
            $h_tmb = $w_tmb * $aspect;
            if ($debug) $msg .= "resizetmb $w_src, $h_src, $w_tmb, $h_tmb$eol ";
            $img_src = self::loadtheimageintocore($sourceFile);
            $img_tmb =
                self::resizeImage($img_src, $w_src, $h_src, $w_tmb, $h_tmb);
            if (!imagejpeg($img_tmb, $fullfileThumb)) //  save new image
                throw new Exception(" E#134 imagejpeg($img_tmb,
                                    $fullfileThumb ) failed ");
            if ($debugImageWork) $msg .= " thumbnail saved to $fullfileThumb $eol";
            imagedestroy($img_tmb); // free memory
            // --------------------------------------------- resize copyright
            if ($w_src != $width) {
                $w_cr = $width;
                $h_cr = $h_src * $aspect;
                $img_src = self::loadtheimageintocore($sourceFile);
                $img_copyright =
                    self::resizeImage($img_src, $w_src, $h_src, $w_cr, $h_cr);
                imagedestroy($img_src); // free memory
                if ($debugImageWork) $msg .= "aspect = $aspect image
                    resized $w_src X $h_src to $w_cr X $h_cr $eol";
            } else {
                $img_copyright = self::loadtheimageintocore($sourceFile);;
                $h_cr = $h_src;
                $w_cr = $w_src;
            }
            // img_copywite has been created.
            if (empty($photographer)) {
                // skip adding the bottom line, just write the file
                if ($debugImageWork) $msg .= "about to write file to $fullFilePhoto ...";
                if (!imagejpeg($img_copyright, $fullFilePhoto))
                    throw new Exception("$msg $errorBeg E#141
                        imagejpeg( $img_copyright, $fullFilePhoto )$errorEnd");
                if ($debugImageWork) $msg .= " imageno border saved to $fullFilePhoto $eol";
                if (!imagedestroy($img_copyright))
                    throw new Exception("$msg $errorBeg E#215
                            imagedestroy( img_copyright ) $errorEnd");
            } else {
                //  ------------------------------------------------ place text
                $h_new = $h_cr + $h_botWhite;
                $imgFinal = imagecreatetruecolor($w_cr, $h_new);
                if (!imagecopy(
                    $imgFinal,
                    $img_copyright,
                    0,
                    0,
                    0,
                    0,
                    $w_src,
                    $h_cr
                ))
                    throw new Exception("E#154
                            imagecopy( $imgFinal, $img_copyright,
                                0, 0, 0, 0, $w_src, $h_cr  failed");
                $white = imagecolorallocate($imgFinal, 255, 255, 255);
                if ($white === false)
                    throw new Exception("E#148
                    imagecolorallocate($imgFinal, 255,255,0)");
                $black = imagecolorallocate($imgFinal, 0, 0, 0); # returns zero
                if ($black === false)
                    throw new Exception("E#147
                    imagecolorallocate($imgFinal, 0,0,0)");
                if (!imagefilledrectangle($imgFinal, 0, $h_cr, $w_src, $h_new, $white))
                    throw new Exception(" $msg E#151
                    imagerectangle(... 0, $h_cr,$w_src, $h_new, $white ) ");
                $left = 2;
                $top = $h_cr + 2;
                $bot = $h_new - 3;
                $copyrightMsg = "Photo by $photographer"; //pictures.shaw-weil.com";
                // use true type fonts. First deterine the actual length
                $bounds = imagettftext($imgFinal, $fontSize, 0, $left, $bot, $black, $fontfile, $copyrightMsg);
                if ($bounds === false)
                    throw new Exception("E#157 imagettftext ($img_copyright,
                                $fontSize, 0, $left, $bot,
                                $black,$fontfile,$copyrightMsg)");
                $w_text = abs($bounds[0] - $bounds[2]);
                $h_text = abs($bounds[1] - $bounds[5]);
                $left = ($w_cr - $w_text) / 2; # center the text left to right
                if ($left < 2) $left = 2;
                $bot = $h_new - 3; # just barely off the bottom.
                # black out the text just drawn, and redraw it centered on the visiable image
                $tmpHeight = $h_cr - $h_botWhite;
                if (!imagefilledrectangle($imgFinal, 0, $h_cr, $w_src, $h_new, $white))
                    throw new Exception("imagefilledrectangle( imgFinal, 0, $h_cr, $w_src $h_new, $white ) ");
                $bounds = imagettftext($imgFinal, $fontSize, 0, $left, $bot, $black, $fontfile, $copyrightMsg);
                if ($bounds === false)
                    throw new Exception("imagettftext (img_copyright, $fontSize,               0, $left, $bot, $black, $fontfile,
                                    $copyrightMsg)");
                if (!imagejpeg($imgFinal, $fullFilePhoto))
                    throw new Exception("imagejpeg( imgFinal, fullFilePhoto ");
                if ($debugImageWork) $msg .= " copyright border saved to $fullFilePhoto $eol";
                if (!imagedestroy($imgFinal))
                    throw new Exception("imagedestroy( img_copyright ");
                if (!imagedestroy($img_src)) // free memory
                    throw new Exception("$msg E#152 imagedestroy( img_sec )");
            } // end of drawing photographer name
        } // end try
        catch (Exception $ex) {
            $msg .= $errorBeg . $ex->getMessage() . $errorEnd;
        }
        return $msg;
    } // end makeImages

    private static function loadtheimageintocore($sourceFile)
    {
        # load the image into core
        global $eol, $errorBeg, $errorEnd;
        $im_src = new Imagick();
        $mime_type = mime_content_type($sourceFile);
        switch ($mime_type) {
            case "image/gif": //   gif -> jpg
                $img_src = imagecreatefromgif($sourceFile);
                break;
            case "image/jpg": //   jpeg -> jpg
            case "image/jpeg": //   jpeg -> jpg
                $img_src = imagecreatefromjpeg($sourceFile);
                break;
            case "image/png": //   png -> jpg
                $img_src = imagecreatefrompng($sourceFile);
                break;
            default:
                throw new Exception(" $errorBeg File '$sourceFile' is
                    mime_type, only GIF, JPG, jpeg or PNG are allowed
                    $errorEnd");
        }
        return $img_src;
    } // end loadtheimageintocore
    private static function resizeImage(
        $img_in,
        $curWidth,
        $curHeight,
        $w_new,
        $h_new
    ) {
        global $eol;
        $debugResize = true;
        $debugExif = true;
        $scalefactor = $w_new / $curWidth;
        $imgout = imagescale($img_in, $scalefactor);
        if (false === $imgout)
            throw new Exception("E#156 failure in resize usine $scalefactor
            as scale factor or $w_new/ $curWidth ");
        return $imgout;
        $img_out = imagecreatetruecolor($w_new, $h_new)
            or die("imagecreatetruecolor($w_new, $h_new)");
        imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $w_new, $h_new, $curWidth, $curHeight)
            or die("imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $w_new, $h_new, $curWidth, $curHeight");
        return $img_out;
    }
} // end class ImageWork
