1

以下は、PHP の写真アップロード スクリプト用に作成した透かし関数です。ファイルの種類をチェックする部分を行うためのより良い方法があるかどうか興味があります。コードのその部分を 2 回使用する必要があることに注意してください。

<?PHP
function watermark($source_file,$source_width,$source_height,$image_type) {
    //first image below will be large then small in 1line if/else
    $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
    //now add the watermark to the image.
    $watermark = imagecreatefromgif($watermarksize);
    switch ($image_type) {
        case 'gif':
            $image = imagecreatefromgif($source_file);
            break;
        case 'jpg':
            $image = imagecreatefromjpeg($source_file);
            break;
        case 'png':
            $image = imagecreatefrompng($source_file);
            break;
        default:
            $image = imagecreatefromjpeg($source_file);
            break;
    }
    //get the dimensions of the watermark
    list($water_width, $water_height) = getimagesize($watermarksize);
    // Water mark process
    $x = $source_width - $water_width - 8; //horizontal position
    $y = $source_height - $water_height - 8; //vertical positon
    // imagesy($image) can be the source images width
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);
    switch ($image_type) {
        case 'gif':
            imagegif($image, $source_file, 90);
            break;
        case 'jpg':
            imagejpeg($image, $source_file, 90);
            break;
        case 'png':
            imagepng($image, $source_file, 90);
            break;
        default:
            imagejpeg($image, $source_file, 90);
            break;
    }
    imagedestroy($image);
    return $source_file;
}
?>
4

1 に答える 1