05-04-26, 03:40 PM
(05-04-26, 02:06 PM)nnnjk كتب : فعلا رابط صورة صحيفة الرياض الصوره غير موجوده
بعد تجربة الكود السابق تبين مايلي
الكود يعمل لاكن لي ملاحظات
1-تحويل جميع الصور الى jpg فليت يحولها لرابطها الاساسي
2- لايختم الصور من نوع webp و avif
اذا كان نوع الملف غير معروف يحولها الى jpg دون ان يختم
PHP كود :
<?php
// تحميل الصورة باستخدام cURL
function downloadImage($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
curl_setopt($ch, CURLOPT_REFERER, "https://www.alriyadh.com/");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$data = curl_exec($ch);
if(curl_errno($ch) || !$data){
curl_close($ch);
return false;
}
curl_close($ch);
return $data;
}
// كشف نوع الملف الحقيقي من البيانات
function getImageType($imageData) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_buffer($finfo, $imageData);
finfo_close($finfo);
return [
'mime' => $mime,
'ext' => match($mime) {
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
'image/avif' => 'avif',
default => 'jpg'
}
];
}
// ختم الصورة باستخدام GD مع دعم جميع الأنواع
function watermark($filePath, $logoPath){
$imageType = getImageType(file_get_contents($filePath));
$ext = $imageType['ext'];
// تحميل الصورة حسب نوعها
$img = match($ext) {
'jpg', 'jpeg' => @imagecreatefromjpeg($filePath),
'png' => @imagecreatefrompng($filePath),
'gif' => @imagecreatefromgif($filePath),
'webp' => function_exists('imagecreatefromwebp') ? @imagecreatefromwebp($filePath) : false,
'avif' => false, // GD لا يدعم AVIF حالياً
default => false
};
if(!$img) return false;
// تحميل الشعار
$logo = @imagecreatefrompng($logoPath);
if(!$logo) return false;
$imgW = imagesx($img);
$imgH = imagesy($img);
$logoW = imagesx($logo);
$logoH = imagesy($logo);
// التأكد من أن الشعار لا يتجاوز حجم الصورة
if($logoW > $imgW || $logoH > $imgH) {
imagedestroy($img);
imagedestroy($logo);
return false;
}
$x = $imgW - $logoW - 10;
$y = $imgH - $logoH - 10;
// تفعيل الشفافية
imagealphablending($img, true);
imagesavealpha($img, true);
imagealphablending($logo, true);
imagesavealpha($logo, true);
// نسخ الشعار مع الحفاظ على الشفافية
imagecopy($img, $logo, $x, $y, 0, 0, $logoW, $logoH);
// حفظ الصورة بنفس النوع الأصلي
$result = match($ext) {
'jpg', 'jpeg' => imagejpeg($img, $filePath, 90),
'png' => imagepng($img, $filePath, 9),
'gif' => imagegif($img, $filePath),
'webp' => function_exists('imagewebp') ? imagewebp($img, $filePath, 90) : false,
default => false
};
imagedestroy($img);
imagedestroy($logo);
return $result;
}
// معالجة النموذج
if($_POST){
$url = trim($_POST['url']);
if(empty($url)) {
die("❌ أدخل رابط الصورة");
}
$imageData = downloadImage($url);
if(!$imageData) {
die("❌ فشل تحميل الصورة (الرابط محمي أو غير صالح)");
}
// إنشاء مجلد التحميل
if(!file_exists("uploads")) {
mkdir("uploads", 0777, true);
}
$imageType = getImageType($imageData);
$name = time() . "." . $imageType['ext'];
$filePath = "uploads/" . $name;
// حفظ الصورة
if(file_put_contents($filePath, $imageData) === false) {
die("❌ فشل حفظ الصورة");
}
// إضافة الختم
if(watermark($filePath, "logo/logo.png")) {
$fileSize = filesize($filePath);
$fileSizeMB = round($fileSize / 1024 / 1024, 2);
echo "✅ تم تحميل وختم الصورة بنجاح!<br>";
echo "<a href='$filePath' target='_blank'><img src='$filePath' style='max-width:500px;max-height:500px;border:2px solid #4CAF50;'></a><br>";
echo "<strong>الحجم: {$fileSizeMB} ميجابايت</strong> | <a href='$filePath' download>تحميل الصورة</a>";
} else {
// إذا فشل الختم، احذف الصورة وعرض رسالة
unlink($filePath);
echo "⚠️ تم تحميل الصورة لكن فشل إضافة الختم<br>";
echo "<a href='$filePath' target='_blank'>عرض الصورة بدون ختم</a>";
}
exit;
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>تحميل وختم الصور المتقدم</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
h3 {
text-align: center;
color: #fff;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
form {
background: rgba(255,255,255,0.1);
padding: 30px;
border-radius: 15px;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
input[type="text"] {
width: 100%;
padding: 15px;
border: none;
border-radius: 10px;
font-size: 16px;
margin-bottom: 20px;
box-sizing: border-box;
background: rgba(255,255,255,0.9);
}
input[type="submit"] {
width: 100%;
padding: 15px;
background: #4CAF50;
color: white;
border: none;
border-radius: 10px;
font-size: 18px;
font-weight: bold;
cursor: pointer;
transition: background 0.3s;
}
input[type="submit"]:hover {
background: #45a049;
}
.supported {
background: rgba(255,255,255,0.1);
padding: 15px;
border-radius: 10px;
margin-top: 20px;
font-size: 14px;
}
</style>
</head>
<body>
<h3>?️ تحميل وختم صورة من رابط (جميع الصيغ)</h3>
<form method="post">
<input type="text" name="url" placeholder="مثال: https://example.com/image.jpg أو .png أو .webp أو .avif" required>
<input type="submit" value="? تحميل وختم الصورة">
</form>
<div class="supported">
✅ <strong>الصيغ المدعومة:</strong> JPG, PNG, GIF, WebP<br>
⚠️ AVIF: يتم تحميلها كـ JPG بدون ختم (GD لا يدعمها)<br>
? يحتفظ بالصيغة الأصلية مع الختم
</div>
</body>
</html>
للعلم فقط الاستضافة لديك لا تدعم مكتبة Imagick
لذلك GD لا يدعم AVIF يتم تحميلها كـ JPG .


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