02-04-26, 12:45 AM
تجربة على الهامش :
هذا كود اخر طالما عندك PHP 5.6
ملف فحص و تشخيص (diagnostic.php)
طريقة الاستخدام :
لا تنسى الاستغفار
هذا كود اخر طالما عندك PHP 5.6
PHP كود :
<?php
/**
* نظام تحميل ومعالجة الصور مع علامة مائية
* الإصدار: 2.0
*/
// إعدادات عامة
set_time_limit(0);
ini_set('memory_limit', '512M');
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
// ============================================================
// الدوال الأساسية
// ============================================================
/**
* تحميل الصورة من الرابط
*/
function download_image($url) {
// فحص وجود cURL
if (!function_exists('curl_init')) {
// استخدام file_get_contents كبديل
$context = stream_context_create([
'http' => [
'timeout' => 60,
'user_agent' => 'Mozilla/5.0'
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]);
$data = @file_get_contents($url, false, $context);
if ($data === false) {
return false;
}
// فحص نوع المحتوى
$content_type = @get_headers($url, 1)['Content-Type'] ?? '';
if (strpos($content_type, 'image') === false && strlen($data) < 100) {
return false;
}
return $data;
}
// استخدام cURL
$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_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
]);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if ($http_code != 200 || empty($data)) {
error_log("Download failed: HTTP $http_code, Error: $curl_error, URL: $url");
return false;
}
return $data;
}
/**
* الحصول على امتداد الصورة
*/
function get_image_extension($url, $data) {
// 1. محاولة من الرابط
$path = parse_url($url, PHP_URL_PATH);
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
// 2. فحص نوع الملف من البيانات
if (empty($ext) || !in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_buffer($finfo, $data);
finfo_close($finfo);
switch ($mime_type) {
case 'image/jpeg':
$ext = 'jpg';
break;
case 'image/png':
$ext = 'png';
break;
case 'image/gif':
$ext = 'gif';
break;
case 'image/webp':
$ext = 'webp';
break;
default:
$ext = 'jpg';
}
}
// تأكد من أن الامتداد مدعوم
$supported = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
if (!in_array($ext, $supported)) {
$ext = 'jpg';
}
return $ext;
}
/**
* إضافة علامة مائية للصورة
*/
function add_watermark($image_path, $ext, $logo_path) {
// فحص وجود الصورة والعلامة
if (!file_exists($image_path) || !file_exists($logo_path)) {
error_log("File not found: Image=" . $image_path . ", Logo=" . $logo_path);
return false;
}
// فحص دعم GD
if (!extension_loaded('gd')) {
error_log("GD extension not loaded");
return false;
}
$ext = strtolower($ext);
// تحميل الصورة الأصلية
$image = null;
switch ($ext) {
case 'jpg':
case 'jpeg':
$image = @imagecreatefromjpeg($image_path);
break;
case 'png':
$image = @imagecreatefrompng($image_path);
break;
case 'gif':
$image = @imagecreatefromgif($image_path);
break;
case 'webp':
if (function_exists('imagecreatefromwebp')) {
$image = @imagecreatefromwebp($image_path);
}
break;
}
if (!$image) {
error_log("Failed to load image: $image_path");
return false;
}
// تحميل العلامة المائية
$logo = @imagecreatefrompng($logo_path);
if (!$logo) {
error_log("Failed to load logo: $logo_path");
imagedestroy($image);
return false;
}
// حساب الأبعاد
$img_w = imagesx($image);
$img_h = imagesy($image);
$logo_w = imagesx($logo);
$logo_h = imagesy($logo);
// إذا كانت الصورة أصغر من العلامة، لا نضيف علامة
if ($img_w <= $logo_w + 10 || $img_h <= $logo_h + 10) {
imagedestroy($image);
imagedestroy($logo);
return true;
}
// وضع العلامة في الزاوية السفلية اليمنى
$pos_x = $img_w - $logo_w - 10;
$pos_y = $img_h - $logo_h - 10;
// دمج الصور
imagealphablending($image, true);
imagecopy($image, $logo, $pos_x, $pos_y, 0, 0, $logo_w, $logo_h);
// حفظ الصورة
$save_result = false;
switch ($ext) {
case 'jpg':
case 'jpeg':
$save_result = @imagejpeg($image, $image_path, 90);
break;
case 'png':
$save_result = @imagepng($image, $image_path, 9);
break;
case 'gif':
$save_result = @imagegif($image, $image_path);
break;
case 'webp':
if (function_exists('imagewebp')) {
$save_result = @imagewebp($image, $image_path, 90);
}
break;
}
imagedestroy($image);
imagedestroy($logo);
return $save_result;
}
/**
* المعالجة الرئيسية للصورة
*/
function process_image($url, $save_path_without_ext, $logo_path) {
// 1. فحص المعلمات
if (empty($url) || empty($save_path_without_ext)) {
error_log("Invalid parameters");
return ['success' => false, 'error' => 'Invalid parameters'];
}
// 2. تحميل الصورة
$image_data = download_image($url);
if (!$image_data) {
return ['success' => false, 'error' => 'Failed to download image'];
}
// 3. تحديد الامتداد
$ext = get_image_extension($url, $image_data);
// 4. إنشاء المجلد إذا لم يكن موجوداً
$dir = dirname($save_path_without_ext);
if (!is_dir($dir)) {
if (!mkdir($dir, 0755, true)) {
return ['success' => false, 'error' => 'Failed to create directory: ' . $dir];
}
}
// 5. حفظ الصورة مؤقتاً
$temp_file = $save_path_without_ext . '_temp.' . $ext;
if (file_put_contents($temp_file, $image_data) === false) {
return ['success' => false, 'error' => 'Failed to save temporary file'];
}
// 6. التحقق من صحة الصورة
$image_info = @getimagesize($temp_file);
if (!$image_info) {
@unlink($temp_file);
return ['success' => false, 'error' => 'Invalid image file'];
}
// 7. إعادة تسمية الملف
$final_file = $save_path_without_ext . '.' . $ext;
if (!rename($temp_file, $final_file)) {
@unlink($temp_file);
return ['success' => false, 'error' => 'Failed to rename file'];
}
// 8. إضافة العلامة المائية (اختيارية)
if (!empty($logo_path) && file_exists($logo_path)) {
$watermark_result = add_watermark($final_file, $ext, $logo_path);
if (!$watermark_result) {
error_log("Warning: Failed to add watermark to $final_file");
}
}
// 9. إرجاع النتيجة
return [
'success' => true,
'file' => $final_file,
'url' => $url,
'extension' => $ext,
'size' => filesize($final_file)
];
}
/**
* دالة مبسطة للاستخدام السريع
*/
function download_and_watermark($url, $save_path, $logo_path = null) {
$result = process_image($url, $save_path, $logo_path);
if ($result['success']) {
return $result['file'];
} else {
error_log("Process failed: " . $result['error']);
return false;
}
}
// ============================================================
// مثال على الاستخدام
// ============================================================
// إذا تم استدعاء الملف مباشرة (وليس ضمن ملف آخر)
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
// إعدادات
$url = "https://example.com/image.webp";
$save = "uploads/2026/04/img_1";
$logo = "uploads/logo.png";
// فحص وجود مجلد uploads
if (!is_dir('uploads')) {
mkdir('uploads', 0755);
echo "Created uploads directory\n";
}
// فحص وجود ملف الشعار
if (!file_exists($logo)) {
echo "Warning: Logo file not found at: $logo\n";
echo "Image will be downloaded without watermark\n\n";
$logo = null;
}
// تنفيذ العملية
echo "Processing image...\n";
$result = process_image($url, $save, $logo);
if ($result['success']) {
echo "Successfully saved:\n";
echo "File: " . $result['file'] . "\n";
echo "Size: " . round($result['size'] / 1024, 2) . " KB\n";
echo "Extension: " . $result['extension'] . "\n";
echo "Original URL: " . htmlspecialchars($result['url']) . "\n";
// عرض الصورة
if (file_exists($result['file'])) {
echo "\n<img src='" . $result['file'] . "' style='max-width:300px; border:1px solid #ddd; padding:5px;'>\n";
}
} else {
echo "Failed: " . $result['error'] . "\n";
}
}
?>PHP كود :
<?php
echo "<h2>Server Environment Check</h2>";
// 1. PHP Information
echo "<h3>1. PHP Information</h3>";
echo "PHP Version: " . phpversion() . "<br>";
echo "Memory Limit: " . ini_get('memory_limit') . "<br>";
echo "Max Execution Time: " . ini_get('max_execution_time') . "<br>";
// 2. Required Extensions
echo "<h3>2. Required Extensions</h3>";
$extensions = ['curl', 'gd', 'fileinfo'];
foreach ($extensions as $ext) {
$loaded = extension_loaded($ext);
echo $ext . ": " . ($loaded ? "Installed" : "Not Installed") . "<br>";
}
// 3. GD Format Support
if (extension_loaded('gd')) {
echo "<h3>3. GD Format Support</h3>";
$gd_info = gd_info();
echo "JPEG: " . ($gd_info['JPEG Support'] ? "Yes" : "No") . "<br>";
echo "PNG: " . ($gd_info['PNG Support'] ? "Yes" : "No") . "<br>";
echo "GIF: " . ($gd_info['GIF Read Support'] ? "Yes" : "No") . "<br>";
echo "WebP: " . (function_exists('imagewebp') ? "Yes" : "No") . "<br>";
}
// 4. Folder Permissions
echo "<h3>4. Folder Permissions</h3>";
$folders = ['uploads', dirname(__FILE__)];
foreach ($folders as $folder) {
if (file_exists($folder)) {
echo $folder . ": Writable " . (is_writable($folder) ? "Yes" : "No") . "<br>";
} else {
echo $folder . ": Not found (will be created automatically)<br>";
}
}
// 5. Test Image Download
echo "<h3>5. Test Image Download</h3>";
$test_url = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
$ch = curl_init($test_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http == 200 && $data) {
echo "Successfully downloaded test image (HTTP $http)<br>";
// Save test image
$test_file = 'uploads/test_image.png';
if (file_put_contents($test_file, $data)) {
echo "Successfully saved test image: $test_file<br>";
echo "<img src='$test_file' style='max-width:200px;'>";
}
} else {
echo "Failed to download test image (HTTP $http)<br>";
}
echo "<h3>Check Complete</h3>";
echo "<p>If all items show 'Yes' or 'Installed', the system is working correctly.</p>";
?>PHP كود :
<?php
// استدعاء ملف النظام
require_once 'image_processor.php';
// استخدام الدالة المبسطة
$result = download_and_watermark(
'https://example.com/image.jpg',
'uploads/2026/04/my_image',
'uploads/logo.png'
);
if ($result) {
echo "Image saved at: " . $result;
} else {
echo "Failed to download image";
}
// استخدام الدالة المتقدمة
$details = process_image(
'https://example.com/image.webp',
'uploads/2026/04/my_image',
'uploads/logo.png'
);
if ($details['success']) {
echo "Saved: " . $details['file'];
echo "Size: " . round($details['size'] / 1024, 2) . " KB";
} else {
echo "Error: " . $details['error'];
}
?>لا تنسى الاستغفار
![[صورة مرفقة: 4360079361.jpg]](https://www.alriyadh.com/media/article/2026/03/05/img/4360079361.jpg)