05-04-26, 01:22 PM
PHP كود :
<?php
error_reporting(E_ALL & ~E_NOTICE);
header('Content-Type: image/png');
define("MDIR", dirname(__FILE__));
// ================== إعدادات ==================
$watermark_text = "الشعافينمنحرب"; // نص الختم
$font = MDIR . "/includes/fonts/arial.ttf"; // الخط
$font_size = 18;
$band_height = 35;
$save_dir = MDIR . "/wmark_/";
// ================== دالة تحميل الصورة ==================
function downloadImage($url, $save_path) {
// محاولة file_get_contents
$data = @file_get_contents($url);
if ($data !== false) {
file_put_contents($save_path, $data);
return true;
}
// إذا فشل نستخدم cURL
if (function_exists('curl_init')) {
$ch = curl_init($url);
$fp = fopen($save_path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return file_exists($save_path);
}
return false;
}
// ================== جلب الصورة ==================
if (!isset($_GET['src'])) {
die('No image');
}
$src = trim($_GET['src']);
$filename = md5($src) . ".img";
$local_image = $save_dir . $filename;
// تحميل الصورة إذا غير موجودة
if (!file_exists($local_image)) {
if (!downloadImage($src, $local_image)) {
die('فشل تحميل الصورة');
}
}
// ================== تحديد نوع الصورة ==================
$info = getimagesize($local_image);
if (!$info) {
die('Invalid image');
}
switch ($info['mime']) {
case 'image/jpeg':
$im = imagecreatefromjpeg($local_image);
break;
case 'image/png':
$im = imagecreatefrompng($local_image);
break;
case 'image/gif':
$im = imagecreatefromgif($local_image);
break;
case 'image/webp':
$im = imagecreatefromwebp($local_image);
break;
case 'image/bmp':
$im = imagecreatefrombmp($local_image);
break;
default:
die('نوع صورة غير مدعوم');
}
// ================== إنشاء طبقة الختم ==================
$width = imagesx($im);
$stamp = imagecreatetruecolor($width, $band_height);
// خلفية سوداء شفافة
$black = imagecolorallocatealpha($stamp, 0, 0, 0, 60);
imagefilledrectangle($stamp, 0, 0, $width, $band_height, $black);
// لون النص
$white = imagecolorallocate($stamp, 255, 255, 255);
// كتابة النص
$y = $font_size + 5;
imagettftext($stamp, $font_size, 0, 10, $y, $white, $font, $watermark_text);
// ================== دمج الختم ==================
$stamp_w = imagesx($stamp);
$stamp_h = imagesy($stamp);
imagecopymerge(
$im,
$stamp,
imagesx($im) - $stamp_w,
imagesy($im) - $stamp_h,
0,
0,
$stamp_w,
$stamp_h,
60
);
// ================== إخراج الصورة ==================
imagepng($im);
// تنظيف
imagedestroy($im);
imagedestroy($stamp);
?>PHP كود :
watermark.php?src=https://example.com/image.jpg
إذا لم يكن موجود، سيفشل الكتابة على الصورة.


![[صورة مرفقة: 177461173141861.gif]](https://up6.cc/2026/03/177461173141861.gif)