01-04-26, 05:44 PM
PHP كود :
<?php
@set_time_limit(0);
@ini_set('memory_limit', '512M');
function download_image($url) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_ENCODING => '',
CURLOPT_HTTPHEADER => [
'Accept: image/webp,image/*,*/*;q=0.8'
],
CURLOPT_USERAGENT => 'Mozilla/5.0'
]);
$data = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http != 200 || !$data) {
return false;
}
return $data;
}
function watermark($file, $ext, $logo) {
if (!file_exists($file) || !file_exists($logo)) return false;
$ext = strtolower($ext);
// تحميل الصورة
switch ($ext) {
case 'jpg':
case 'jpeg':
$img = @imagecreatefromjpeg($file);
break;
case 'png':
$img = @imagecreatefrompng($file);
break;
case 'gif':
$img = @imagecreatefromgif($file);
break;
case 'webp':
if (function_exists('imagecreatefromwebp')) {
$img = @imagecreatefromwebp($file);
} else {
return false;
}
break;
default:
return false;
}
if (!$img) return false;
$logo_img = @imagecreatefrompng($logo);
if (!$logo_img) return false;
$bw = imagesx($img);
$bh = imagesy($img);
$lw = imagesx($logo_img);
$lh = imagesy($logo_img);
if ($bw < $lw + 10 || $bh < $lh + 10) return false;
$x = $bw - $lw - 5;
$y = $bh - $lh - 5;
imagealphablending($img, true);
imagecopy($img, $logo_img, $x, $y, 0, 0, $lw, $lh);
// حفظ الصورة
switch ($ext) {
case 'jpg':
case 'jpeg':
imagejpeg($img, $file, 90);
break;
case 'png':
imagepng($img, $file, 9);
break;
case 'gif':
imagegif($img, $file);
break;
case 'webp':
imagewebp($img, $file, 90);
break;
}
imagedestroy($img);
imagedestroy($logo_img);
return true;
}
function process_image($url, $save_path, $logo) {
// تحميل الصورة
$data = download_image($url);
if (!$data) return false;
// استخراج الامتداد
$ext = strtolower(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION));
if (!$ext) $ext = 'jpg';
// دعم WebP
if (!in_array($ext, ['jpg','jpeg','png','gif','webp'])) {
$ext = 'jpg';
}
// إنشاء المجلد
if (!is_dir(dirname($save_path))) {
mkdir(dirname($save_path), 0755, true);
}
$file = $save_path . '.' . $ext;
// حفظ أولي
file_put_contents($file, $data);
// تحقق من الصورة
$img = @imagecreatefromstring($data);
// إذا فشل (خصوصاً WebP)
if (!$img && $ext == 'webp' && function_exists('imagecreatefromwebp')) {
$img = @imagecreatefromwebp($file);
}
if (!$img) {
unlink($file);
return false;
}
imagedestroy($img);
// إضافة watermark
watermark($file, $ext, $logo);
return $file;
}
?>طريقة الاستخدام :
PHP كود :
$url = "https://example.com/image.webp";
$save = "uploads/2026/04/img_1";
$logo = "uploads/logo.png";
$result = process_image($url, $save, $logo);
if ($result) {
echo "تم الحفظ: " . $result;
} else {
echo "فشل";
}
جرّب و خبرني


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