Yapılan bazı projelerde içeriklerin korunması amacıyla yayınlanan resimlere imza niteliğinde bir logo eklenmesi istenebiliyor. Bu uygulamada PHP kodlarıyla dinamik olarak yüklenen resimlere logo basma bir diğer anlamıyla resimlere filigran basma olayını inceliyoruz.
Uygulama için sadece resim yüklenecek basit bir formdan oluşan index.html dosyasını yazıyoruz.
index.php
<!-- Formun yönleneceği action url'si watermark.php isimli dosya--> <form action="watermark.php" method="post" enctype="multipart/form-data"> <input type="file" name="File1"><br> <input type="submit" value="Submit File" /> </form>
index.html ile alınacak resim dosyası , watermark.php dosyaya ulaşıyor.
watermark.php
<php
define( 'WATERMARK_OVERLAY_IMAGE', 'serpito.png' ); // Resim uzerine basilacak logo
define( 'WATERMARK_OVERLAY_OPACITY', 50 ); //Opacity - seffaflik
define( 'WATERMARK_OUTPUT_QUALITY', 90 ); // Resim kalitesi
function create_watermark( $source_file_path, $output_file_path )
{
list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );
if ( $source_type === NULL )
{
return false;
}
switch ( $source_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $source_file_path );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $source_file_path );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $source_file_path );
break;
default:
return false;
}
$overlay_gd_image = imagecreatefrompng( WATERMARK_OVERLAY_IMAGE );
$overlay_width = imagesx( $overlay_gd_image );
$overlay_height = imagesy( $overlay_gd_image );
imagecopymerge(
$source_gd_image,
$overlay_gd_image,
$source_width - $overlay_width,
$source_height - $overlay_height,
0,
0,
$overlay_width,
$overlay_height,
WATERMARK_OVERLAY_OPACITY
);
imagejpeg( $source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY );
imagedestroy( $source_gd_image );
imagedestroy( $overlay_gd_image );
}
//--------------------------------
// Dosya Analiz
//--------------------------------
define( 'UPLOADED_IMAGE_DESTINATION', 'originals/' );
define( 'PROCESSED_IMAGE_DESTINATION', 'images/' );
function process_image_upload( $Field )
{
$temp_file_path = $_FILES[ $Field ][ 'tmp_name' ];
$temp_file_name = $_FILES[ $Field ][ 'name' ];
list( , , $temp_type ) = getimagesize( $temp_file_path );
if ( $temp_type === NULL )
{
return false;
}
switch ( $temp_type )
{
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
$processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace( '/\\.[^\\.]+$/', '.jpg', $temp_file_name );
move_uploaded_file( $temp_file_path, $uploaded_file_path );
$result = create_watermark( $uploaded_file_path, $processed_file_path );
if ( $result === false )
{
return false;
}
else
{
return array( $uploaded_file_path, $processed_file_path );
}
}
//--------------------------------
// Fonksiyon sonu
//--------------------------------
$result = process_image_upload( 'File1' );
if ( $result === false )
{
echo '&lt;br&gt;Dosya isleme sirasinda bi hata olustu!';
}
else
{
echo 'Orjinal resim bu linkte <a href="' . $result[ 0 ] . '" target="_blank">' . $result[ 0 ] . '</a>;
echo '<br>Filigran eklenen resim burada <a href="' . $result[ 1 ] . '" target="_blank">' . $result[ 1 ] . '</a>';
}
?>
Uygulamayı denediğim alttaki resim aşağıdaki gibi sağ-alttaki serpito.png logosu ile baskılandı.

Uygulama dosyalarının arşivini zip halinde indirmek için tıklayınız.


4 Comments
Pingback: Anonim
teşekkür ederim işime yaradı
burada iki resmi birleştiren kodlar hangisi? rica etsem açıklarmısın.
teşekkürler güzel yazı olmuş. siteni ilgiyle izliyorum.