Explore سلام Save Share Download ▾ ReportJump to turn (43) 1 سلام 2 من میخواستم یک آدرس درست کنم که داخل اش کاربر یک لینک قرار بده و یک لینک جدید از 3 یکی از بچه ها برام انجام داد فایل زیپ داد گفت آپلود کن توی هاست اوک ولی انجام نم 4 متن کد میگم 5 یک فایل زیپ داد حاوی چند تا فایل و یک پوشه به اسم storage و یک فایل به اسم downl 6 لینک داخل کادر قرار میدم تا آپلود بشه https://www.aisada.ir/hamed/upload.php این 7 من نمیدونم هیچی بلد نیستم اگر میدونی یک کد یک فایل کامل درست کن به من بگو جایگز 8 میگه لینک ما معتبر است حتما باید فرمت خاص از فایل باشه من عکس با فرمت png آپلود 9 هر فایلی آپلود بشه ببین کد کامل یکجا درست کن بفرست من جایگزین کنم 10 آپلود میکنم میره به این صفحه https://www.aisada.ir/hamed/upload.php و نوشته میش 11 کد کامل بده جایگزین کنم هر بار 12 مثل قبله ببین فایل آپلود نمیکنه که لینک بده آیا همچین کاری میشه 13 نشد ببین بقیه کد ها درسته یک فایل زیپ داد حاوی چند تا فایل و یک پوشه به اسم stor 14 نشد همچین کاری میشه اصلا 15 چجوری بفهمم سطح دسترسی بازه 16 755 است تغییر بدم 17 دسترسی رو باز کردم فرقی نکرد https://www.aisada.ir/hamed/upload.php روی آپلود کل 18 کد جوری درست کن که روی آپلود کلیک کنم یک اسپینر بیاد و همونجا فرایند نشون بده و 19 میگه لینک نامعتبر است https://uploadkon.ir/uploads/cc541924Screenshot-۲۰۲۴-۱۱-۱۹ 20 الان شد اما فقط همون فایل نشون میده و تصویر دیگری آپلود نمیشه کاری کن هر تصویر ب 21 شد کاری کن تصاویر با فرمت webp هم بشه 22 عالی شد خیلی ممنون اوکی شد ببین الان من میخواستم داخل یک صفحه اچ تی ام ال من این 23 کد کامل یکجا درست کن بفرست 24 من گفتم کد بزرگی که همین پیشتر فرستادم کامل درست کن که فرایندی که گفتم انجام بده 25 body, html { margin: 0; padding: 0; height: 100%; overflow: auto; background-col 26 کد منو دارم میگم کامل بفرست مخفی سازی داشت و نوار پیشرفت داشت کد کامل کامل 27 چیکار کردی سایت گرادیو باز نمیشه مثل قبل باشه همه چی فقط لینک دانلود فرق کنه 28 body, html { margin: 0; padding: 0; height: 100%; overflow: auto; background-col 29 روی ذخیره تصویر که کلیک میکنم میکنه در حال آپلود تصویر خب اما لینک نشون نمیده 30 بگو چی بدم تا کامل کنی کد 31 body, html { margin: 0; padding: 0; height: 100%; overflow: auto; background-col 32 ببین کد کامل یکجا دوباره درست کن بدون هیچ مشکلی که روی ذخیره میزنم داخل یک اعلان 33 https://www.aisada.ir/hamed/index.php این سایت کنه که اینجا تصاویر آپلود میکردیم 34 کد کامل بده مخفی سازی داشت متن داشت کد اول ببین دوباره body, html { margin: 0; p 35 کد کامل بده دهنم سرویس شد 36 من این کد body, html { margin: 0; padding: 0; height: 100%; overflow: aut 37 عالی این یکی کد منم درست کن که دکمه ذخیره تصویر داشته باشه مثل اون body, html { 38 این کد کامل بده همه چی باشه حتی مخفی سازی ها و نمایش متن ها همه چی 39 body, html { margin: 0; padding: 0; height: 100%; overflow: auto; background-col 40 میشه این دوتا کد با هم مخلوط کنی نیاز نباشه جدا جدا داخل فایل ها قرار بدیم 41 کد اچ تی ام ال هم میشه 42 به من بگو میشه هر دو کد در یک کد اچ تی ام ال قرار بدیم 43 فقط کوتاه جواب بده داخل کد اچ تی ام ال میشه یا نه Assistant2024-11-19 11:16 UTC Copy response
سلام! چطور میتوانم کمکتان کنم؟
من میخواستم یک آدرس درست کنم که داخل اش کاربر یک لینک قرار بده و یک لینک جدید از هاست خودم بگیره مثلاً لینک مستقیم یک عکس بده لینک مستقیم عکس از دامنه سایت خودم دریافت کنه
Assistant2024-11-19 11:17 UTC Copy response
Copy first prompt Copy first response
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 43 (86 messages)
Language fa
Conversation date Nov 19, 2024
Added to archive Nov 16, 2026
ID 01m7h429zghyqycjy4a69ag3xhSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: d7a309481009973b29328e856a0ceb3e
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
برای انجام این کار، شما نیاز به چندین مرحله مختلف دارید. این پروژه شامل برنامهنویسی بخش سرور، مدیریت فایلها و اطمینان از امنیت و کارایی مناسب میشود. به طور کلی، این روند به مراحل زیر تقسیم میشود:
راهاندازی سرور : ابتدا به یک سرور یا فضای میزبانی نیاز دارید که بتوانید سایت خود را روی آن اجرا کنید. این سرور ممکن است فیزیکی یا یک سرور مجازی در یکی از سرویسهای ابری باشد.
برنامهنویسی بخش سرور : شما باید یک نرمافزار سمت سرور بنویسید که لینکهای ورودی را دریافت کند و به آنها پاسخ دهد. به عنوان مثال از زبانهایی مانند PHP، Node.js، Python (Django یا Flask)، یا دیگر زبانهای برنامهنویسی سمت سرور استفاده کنید.
ذخیرهسازی فایلها : زمانی که یک کاربر لینکی از یک فایل (مثلاً تصویر) ارسال میکند، باید آن فایل را دریافت کرده و آن را بر روی سرور خود ذخیره کنید. این کار میتواند شامل بارگیری فایل از یک URL و سپس ذخیرهسازی آن در فضای میزبانی شما باشد.
تولید لینک جدید : پس از ذخیرهسازی فایل روی سرور، شما باید یک لینک جدید از دامنه خودتان برای دسترسی به این فایل ایجاد کنید. معمولاً این شامل ایجاد یک endpoint در API یا یک مسیر دسترسی به فایل در URL سایت شما است.
مدیریت و امنیت : باید مطمئن شوید که فایلهای آپلود شده را به صورت ایمن مدیریت میکنید. این شامل اسکن فایلها برای وجود نرمافزارهای مخرب ممکن است و اطمینان از اینکه کاربران تصادفی نمیتوانند به هر فایل یا داده دلخواهی دسترسی پیدا کنند.
یک چارچوب ساده در صورت استفاده از PHP به صورت زیر است:
php Copy code
<?php
if (isset($_POST['url'])) {
$url = $_POST['url'];
$filename = basename($url);
$filepath = 'uploads/' . $filename;
if (file_put_contents($filepath, file_get_contents($url))) {
echo 'Your new link is: http://yourdomain.com/' . $filepath;
} else {
echo 'Failed to download the file.';
}
}
?>
<form method="post">
<input type="text" name="url" placeholder="Enter URL" />
<input type="submit" value="Get New Link" />
</form>
این یک نمونه بسیار ساده است و برای استفاده در محیط تولید به بررسیهای امنیتی و بهبود نیاز دارد. حتماً از تکنیکهای امنیتی مانند محدود کردن اندازه فایلها، فیلتر کردن نوع فایلها، و بررسی ورودیهای کاربر استفاده کنید.
یکی از بچه ها برام انجام داد فایل زیپ داد گفت آپلود کن توی هاست اوک ولی انجام نمیشه من فایل هارو میفرستم برات دونه دونه برسی کن
Assistant2024-11-19 11:18 UTC Copy response
متاسفانه، نمیتوانم فایلها را به طور مستقیم دریافت یا بررسی کنم. با این حال، اگر مشکلی در اسکریپت وجود دارد یا نیاز به کمک در مورد نحوه تنظیم آن دارید، میتوانید کد یا توضیحات مربوط به خطاها را اینجا به اشتراک بگذارید تا بتوانم به شما کمک کنم مشکلات را شناسایی و حل کنید. اگر با پیام خطا یا مشکلی مواجهاید، لطفاً جزئیات بیشتری ارائه کنید.
Assistant2024-11-19 11:18 UTC Copy response
البته، لطفاً کد را اینجا به اشتراک بگذارید و اگر با ارور یا مشکلی مواجه شدهاید، توضیح دهید که چه خطایی دریافت میکنید یا چه چیزی آنطور که انتظار دارید عمل نمیکند. به این ترتیب میتوانم بهتر به شما کمک کنم.
یک فایل زیپ داد حاوی چند تا فایل و یک پوشه به اسم storage و یک فایل به اسم download.php با این کد <?php
$storagePath = DIR . '/storage/';
// حذف فایلهای قدیمی
deleteOldFiles($storagePath);
if (isset(_GET['file'])) {
fileName = basename(G E T [ ′ f i l e ′ ] ) ; _GET['file']);
G E T [ ′ f i l e filePath = fileName;
Show full message · 110 lines Assistant2024-11-19 11:31 UTC Copy response
با توجه به کدی که به اشتراک گذاشتید، هر فایل عملکرد خاصی دارد:
index.php : فرم HTML را برای وارد کردن لینک فایل ارائه میدهد.
upload.php : این فایل هنگامی که فرم ارسال میشود، درخواست POST را پردازش و فایل را از لینک دانلود میکند و در پوشه storage ذخیره میکند.
download.php : این فایل مسئول ارائه لینک دانلود مستقیم به فایل ذخیره شده است.
functions.php : شامل یک تابع برای حذف فایلهای قدیمیتر از ۳۰ دقیقه (۱۸۰۰ ثانیه) در پوشه storage است.
برای مشکلیابی، لطفاً به بررسی موارد زیر بپردازید:
مطمئن شوید که پوشه storage با سطح دسترسی مناسب (قابل نوشتن) روی سرور شما وجود دارد.
مطمئن شوید که تمام فایلها در یک پوشه قرار دارند و مسیر require_once 'functions.php'; به درستی به فایل functions.php اشاره میکند.
اگر هنگام اجرای کد به خطا خوردید، متن کامل خطا را بررسی کنید. خطاهای مربوط به دسترسی فایل یا خطاهای پیاچپی ممکن است در لاگهای ارور سرور قابل مشاهده باشد.
ابزارهای ini_set و error_reporting پیاچپی میتوانند جهت نمایش خطاها هنگام توسعه مفید باشند.
اگر باز هم مشکلی داشتید، لطفاً اطلاعات دقیقتر و جزئیات مربوط به خطاها یا رفتار غیرمنتظره را ارائه دهید تا بتوانم بهتر شما را راهنمایی کنم.
Assistant2024-11-19 11:33 UTC Copy response
به نظر میرسد مشکلی در اسکریپت upload.php شما وجود دارد که علت عدم دانلود و ذخیره فایل به درستی و ارائه لینک دانلود است. برای شناسایی مشکل، میتوانید مراحل زیر را دنبال کنید:
بررسی خطا : ابتدا باید مطمئن شوید که هرگونه خطای پیاچپی بر روی سرور شما ثبت یا نمایش داده میشود. میتوانید تنظیمات پیاچپی را برای نمایش خطاها به کمک کد زیر در ابتدای فایل upload.php قرار دهید:
php Copy code
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
این کار به شما کمک میکند تا خطاهایی که ممکن است در فرآیند اجرای کد وجود داشته باشد را مشاهده کنید.
بررسی دسترسی پوشه : مطمئن شوید که پوشه storage قابل نوشتن است. باید سطح دسترسی آن طوری باشد که اسکریپت قادر باشد فایلها را در آن ذخیره کند (مثلاً سطح دسترسی 0777 برای تست).
اعتبار سنجی URL : از آنجایی که ممکن است filter_input قادر به اعتبارسنجی URL نباشد، میتوانید با یک روش سادهتر این کار را تست کنید:
php Copy code
$fileUrl = $_POST['file_url'];
اگر مشکل حل شد، به احتمال زیاد مسئله از اعتبارسنجی URL بوده است.
چک کردن محتوای فایل : میتوانید آزمایش کنید که آیا واقعا محتوای فایل دانلود میشود یا خیر:
php Copy code
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
die("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent)) {
// نمایش لینک
}
با این تغییرات و بررسی خطاها، امیدوارم مشکل شما حل شود. اگر خطایی رخ میدهد که با کدهای فوق حل نشده است، لطفاً متن دقیق خطا را گزارش کنید.
من نمیدونم هیچی بلد نیستم اگر میدونی یک کد یک فایل کامل درست کن به من بگو جایگزین کنم
Assistant2024-11-19 11:35 UTC Copy response
بسیار خوب، من میتوانم یک اسکریپت ساده برایتان پیشنهاد دهم که وقتی یک URL را وارد کنید، فایل را دانلود کرده و یک لینک مستقیم برای دانلود از سرور ارائه دهد. همچنین، این اسکریپت فایلهای قدیمی را پاک خواهد کرد.
مراحل:
یک فایل PHP با نام upload.php ایجاد کنید و محتوای زیر را درون آن قرار دهید:
php Copy code
<?php
// تنظیمات
$storagePath = __DIR__ . '/storage/'; // مسیر پوشه ذخیرهسازی
$maxAgeInSeconds = 1800; // حداکثر سن فایلهای قابل نگهداری
// اطمینان از وجود پوشه ذخیرهسازی
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
// تابع حذف فایلهای قدیمی
function deleteOldFiles($path, $maxAgeInSeconds) {
$files = glob($path . '*');
foreach ($files as $file) {
if (is_file($file) && time() - filemtime($file) > $maxAgeInSeconds) {
unlink($file);
}
}
}
deleteOldFiles($storagePath, $maxAgeInSeconds);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = filter_input(INPUT_POST, 'file_url', FILTER_VALIDATE_URL);
if (!$fileUrl) {
die("لینک نامعتبر است.");
}
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
die("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent)) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='{$_SERVER['PHP_SELF']}?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
}
if (isset($_GET['file'])) {
$fileName = basename($_GET['file']);
$filePath = $storagePath . $fileName;
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
}
?>
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
میگه لینک ما معتبر است حتما باید فرمت خاص از فایل باشه من عکس با فرمت png آپلود کردم
Assistant2024-11-19 11:42 UTC Copy response
اگر پیام خطا میگوید "لینک نامعتبر است"، دلیل آن احتمالاً خطایی در فرایند اعتبارسنجی URL است. بخش filter_input برای اعتبارسنجی URL، ممکن است در برخی موارد نتواند لینکها را به درستی شناسایی کند. همچنین، این مرحله بررسی نمیکند که آیا فایل به فرمت خاصی است یا خیر، بلکه فقط URL را بررسی میکند. برای اطمینان، میتوانید توسط نمایش خطا یا اصلاح کد مشکل را برطرف کنید.
تغییر در اعتبارسنجی URL
کدی که در اعتبارسنجی URL استفاده میشود را به شکل سادهتری تنظیم کنید. به جای استفاده از filter_input، میتوان از filter_var مستقیم استفاده کرد:
در بخش مربوط به اعتبارسنجی URL در فایل upload.php، کد زیر را جایگزین کنید:
php Copy code
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = $_POST['file_url'];
if (!filter_var($fileUrl, FILTER_VALIDATE_URL)) {
die("لینک نامعتبر است.");
}
// ...
}
بررسی فرمت فایل
اگر میخواهید به طور خاص فرمت فایلهایی که آپلود میشود را کنترل کنید (مثلاً اطمینان حاصل کنید که فقط تصاویر با فرمت PNG آپلود میشوند)، میتوانید با بررسی پسوند فایل این کار را انجام دهید:
در ابتدای بلوک که file_put_contents را برای ذخیره فایل فراخوانی میکند، کدی به شکل زیر اضافه کنید:
php Copy code
$allowedExtensions = ['png'];
$fileExtension = pathinfo(parse_url($fileUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
if (!in_array(strtolower($fileExtension), $allowedExtensions)) {
die("فقط فرمتهای PNG مجاز به آپلود هستند.");
}
با این تغییرات، اسکریپت شما هم URL های معتبر را بهتر تشخیص میدهد و اطمینان حاصل میکند که فایلها فقط برای فرمتهای مجاز آپلود و ذخیره میشوند. اگر همچنان با مشکل مواجه شدید، لطفاً جزئیات دقیقتری از خطا را به اشتراک بگذارید.
هر فایلی آپلود بشه ببین کد کامل یکجا درست کن بفرست من جایگزین کنم
Assistant2024-11-19 11:43 UTC Copy response
بسیار خوب، من کد کاملی برای شما تهیه میکنم که به شما امکان میدهد هر نوع فایلی را از یک URL معتبر دانلود کرده و بر روی سرور ذخیره کنید. این کد شامل یک فرم ساده برای وارد کردن URL و یک اسکریپت برای دریافت و ذخیره فایل است.
مراحل انجام کار:
یک فایل PHP به نام upload.php ایجاد کنید.
محتوای زیر را درون این فایل قرار دهید:
php Copy code
<?php
// تنظیمات
$storagePath = __DIR__ . '/storage/';
$maxAgeInSeconds = 1800; // حداکثر سن فایلهای قابل نگهداری در ثانیه (۳۰ دقیقه)
// اطمینان از وجود پوشه ذخیرهسازی
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
// تابع حذف فایلهای قدیمی
function deleteOldFiles($path, $maxAgeInSeconds) {
$files = glob($path . '*');
foreach ($files as $file) {
if (is_file($file) && time() - filemtime($file) > $maxAgeInSeconds) {
unlink($file);
}
}
}
// حذف فایلهای قدیمی
deleteOldFiles($storagePath, $maxAgeInSeconds);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = $_POST['file_url'];
if (!filter_var($fileUrl, FILTER_VALIDATE_URL)) {
die("لینک نامعتبر است.");
}
// نام فایل تصادفی برای ذخیره
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// دانلود فایل
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
die("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent)) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='{$_SERVER['PHP_SELF']}?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
}
if (isset($_GET['file'])) {
$fileName = basename($_GET['file']);
$filePath = $storagePath . $fileName;
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>آپلود از لینک</title>
<style>
html, body {
text-align: right;
direction: rtl;
}
</style>
</head>
<body>
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
</body>
</html>
Assistant2024-11-19 11:46 UTC Copy response
اگر پیام "لینک نامعتبر است" را دریافت میکنید، ممکن است مشکل از نحوه اعتبارسنجی URL باشد. ابتدا مطمئن شوید که URL وارد شده واقعا معتبر است و به طور نادرست تایپ نشده است.
برای اطمینان از درستی اسکریپت و اینکه مشکل از کد اعتبارسنجی URL نیست، بیایید کدی که اعتبار URL را بررسی میکند را با نسخه سادهتری جایگزین کنیم:
مراحل انجام کار:
فایل upload.php خود را باز کنید.
بخش مربوط به دریافت و اعتبارسنجی URL را با کد زیر جایگزین کنید:
php Copy code
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = $_POST['file_url'];
// بررسی سادهتر برای صحت URL
if (!filter_var($fileUrl, FILTER_VALIDATE_URL)) {
die("لینک نامعتبر است.");
}
// ادامه کد
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
die("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent)) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='{$_SERVER['PHP_SELF']}?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
}
حالا دوباره سعی کنید یک URL معتبر از فایل را وارد کنید و بررسی کنید که آیا مشکل حل شده است یا خیر.
نکات:
توجه داشته باشید که اگر همچنان با مشکل مواجه هستید، ممکن است مشکل از جایی دیگر باشد، مانند عدم دسترسی سرور به اینترنت یا عدم امکان خواندن دادهها از URL خاص به دلایلی مانند محدودیتهای فایروال یا تنظیمات سرور. بررسی کنید که آیا میتوانید از همان سرور با ابزارهایی مانند wget یا curl فایلها را دانلود کنید یا خیر.
کد کامل بده جایگزین کنم هر بار
Assistant2024-11-19 11:47 UTC Copy response
بسیار خوب، بیایید کد کامل را بازنویسی کنیم تا مطمئن شویم که URLها به درستی پردازش و فایلها دانلود و ذخیره میشوند. در این کد، ما از یک روش ساده برای اعتبارسنجی URL و دانلود فایلها استفاده میکنیم:
کد کامل:
یک فایل به نام upload.php ایجاد کنید و محتوای زیر را درون آن قرار دهید:
php Copy code
<?php
// تعیین مسیر پوشه ذخیرهسازی
$storagePath = __DIR__ . '/storage/';
$maxAgeInSeconds = 1800; // 30 دقیقه
// اطمینان از وجود پوشه ذخیرهسازی
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
// تابع برای حذف فایلهای قدیمی
function deleteOldFiles($path, $maxAgeInSeconds) {
$files = glob($path . '*');
foreach ($files as $file) {
if (is_file($file) && time() - filemtime($file) > $maxAgeInSeconds) {
unlink($file);
}
}
}
// حذف فایلهای قدیمی
deleteOldFiles($storagePath, $maxAgeInSeconds);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = $_POST['file_url'];
// بررسی ساده URL
if (!filter_var($fileUrl, FILTER_VALIDATE_URL)) {
die("لینک نامعتبر است.");
}
// ایجاد نام فایل خاص و منحصر به فرد
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// دانلود فایل
$fileContent = @file_get_contents($fileUrl);
if ($fileContent === false) {
die("خطایی در دانلود فایل وجود دارد.");
}
// ذخیره فایل
if (file_put_contents($filePath, $fileContent)) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='{$_SERVER['PHP_SELF']}?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
}
if (isset($_GET['file'])) {
$fileName = basename($_GET['file']);
$filePath = $storagePath . $fileName;
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>آپلود از لینک</title>
<style>
html, body {
text-align: right;
direction: rtl;
}
</style>
</head>
<body>
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
</body>
</html>
مثل قبله ببین فایل آپلود نمیکنه که لینک بده آیا همچین کاری میشه
Assistant2024-11-19 11:49 UTC Copy response
به نظر میرسد که چالش موجود به فرایند دانلود فایل از آدرس URL و ذخیره آن مربوط میشود. ممکن است مشکل از یکی از دلایل زیر باشد:
خود URL : مطمئن شوید که URL دسترسی به فایل بهدرستی نوشته شده باشد و فایل برای دانلود در دسترس باشد.
دسترسی سرور : بررسی کنید که سرور شما دسترسی به اینترنت دارد و میتواند فایلها را دانلود کند.
تنظیمات PHP : ممکن است برخی تنظیمات PHP باعث محدودیت در دانلود فایلها شوند (مانند allow_url_fopen).
با این حال، بیایید کد را بهروزرسانی کنیم تا مطمئن شویم که بهطور صریحتری کار میکند:
بهروزرسانی کد برای بهتر مدیریت خطا
فایل upload.php را با کد زیر بهروزرسانی کنید:
نشد ببین بقیه کد ها درسته یک فایل زیپ داد حاوی چند تا فایل و یک پوشه به اسم storage و یک فایل به اسم download.php با این کد <?php
$storagePath = DIR . '/storage/';
// حذف فایلهای قدیمی
deleteOldFiles($storagePath);
if (isset(_GET['file'])) {
fileName = basename(G E T [ ′ f i l e ′ ] ) ; _GET['file']);
G E T [ ′ f i l e filePath = fileName;
Show full message · 110 lines Assistant2024-11-19 11:51 UTC Copy response
به نظر میرسد مشکل به دلیل استفاده نادرست از ثابت DIR به جای __DIR__ برای تعیین مسیر پوشه storage است. __DIR__ مقدار صحیح دایرکتوری جاری فایل را برمیگرداند و باید در اینجا استفاده شود.
در ادامه، کدهای ارائه شده را با اصلاحات لازم میبینید. لطفاً در هر یک از فایلها تغییرات زیر را اعمال کنید:
download.php
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
// حذف فایلهای قدیمی
deleteOldFiles($storagePath);
if (isset($_GET['file'])) {
$fileName = basename($_GET['file']);
$filePath = $storagePath . $fileName;
if (file_exists($filePath)) {
// تنظیم هدرهای دانلود
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
}
function deleteOldFiles($path, $maxAgeInSeconds = 1800) {
$files = glob($path . '*');
foreach ($files as $file) {
if (is_file($file) && time() - filemtime($file) > $maxAgeInSeconds) {
unlink($file);
}
}
}
functions.php
php Copy code
<?php
// تابع حذف فایلهای قدیمی
function deleteOldFiles($path, $maxAgeInSeconds = 1800) {
if (!is_dir($path)) {
die("مسیر $path معتبر نیست یا وجود ندارد.");
}
$files = glob($path . '*'); // دریافت تمام فایلها در پوشه
foreach ($files as $file) {
if (is_file($file)) {
$fileTime = filemtime($file); // زمان آخرین تغییر فایل
$currentTime = time(); // زمان فعلی
$age = $currentTime - $fileTime;
if ($age > $maxAgeInSeconds) {
unlink($file); // حذف فایل
}
}
}
}
Assistant2024-11-19 11:57 UTC Copy response
بله، این کاری که در حال تلاش برای انجام آن هستید بهطور کلی قابل انجام است، یعنی دانلود یک فایل از طریق URL و ذخیرهسازی آن در هاست خود پیش از ارائه یک لینک دانلود مستقیم. اگر هنوز با چالش مواجه هستید، بیایید برخی از موارد پایهای زیر را دوباره چک کنیم:
سطح دسترسی لازم برای پوشه ذخیرهسازی : از طریق ابزارهایی نظیر FTP یا مدیریت فایلهای هاست، مطمئن شوید که سطح دسترسی پوشه storage به گونهای تنظیم شده باشد که امکان نوشتن در آن وجود داشته باشد (مثلاً سطح دسترسی 0777 برای تست).
فعالسازی allow_url_fopen: از طریق فایل php.ini، اطمینان حاصل کنید که گزینه allow_url_fopen فعال است تا قابلیت استفاده از توابعی مانند file_get_contents برای دانلود از URL فعال باشد.
تست دستی دانلود: به صورت دستی از طریق مرورگر یا ابزارهایی نظیر curl یا wget، بررسی کنید که آیا سرور شما میتواند فایلهای مورد نظر را از URL دانلود کند یا خیر. این میتواند به شناسایی مشکلات در ارتباطات اینترنتی یا محدودیتهای فایروال کمک کند.
بررسی خطاها و لاگهای سرور : پیامهای خطای ارائه شده در کد PHP و همچنین لاگهای خطای سرور میتوانند به شما در شناسایی مشکلات خاص کمک کنند.
کد ساده شده برای بررسی:
این نسخه از کد باید بسیار ساده عمل کند. آن را در فایل upload.php قرار دهید:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = filter_input(INPUT_POST, 'file_url', FILTER_VALIDATE_URL);
if (!$fileUrl) {
die("لینک نامعتبر است.");
}
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
try {
$fileContent = @file_get_contents($fileUrl);
if ($fileContent === false) {
throw new Exception("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent) === false) {
throw new Exception("مشکلی در ذخیره فایل به وجود آمد.");
}
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='download.php?file={$fileName}'>دانلود</a>";
} catch (Exception $e) {
die("خطا: " . $e->getMessage());
}
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>آپلود از لینک</title>
<style>
html, body {
text-align: right;
direction: rtl;
}
</style>
</head>
<body>
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
</body>
</html>
چجوری بفهمم سطح دسترسی بازه
Assistant2024-11-19 12:05 UTC Copy response
برای بررسی و تغییر سطح دسترسی (permissions) یک پوشه یا فایل در سرور، میتوانید از ابزار یا روشهای زیر استفاده کنید. توجه داشته باشید که دسترسیها را نباید بیش از حد باز تنظیم کنید، زیرا این کار میتواند به دلایل امنیتی مشکلاتی ایجاد کند.
چگونه سطح دسترسی را بررسی کنیم:
1. استفاده از FTP Client:
از یک برنامه FTP Client مانند FileZilla استفاده کنید.
به سرور خود با استفاده از اطلاعات FTP وارد شوید.
به دایرکتوری که میخواهید سطح دسترسی آن را بررسی کنید بروید.
بر روی پوشه storage راست کلیک کرده و گزینه "File Permissions" یا "Permissions" را انتخاب کنید.
در اینجا میتوانید سطح دسترسی فعلی پوشه را مشاهده کنید. مجوز 0777 به معنای دسترسی کامل خواندن، نوشتن و اجرای فایل برای کاربر، گروه و دیگران است. برای تست میتوانید این سطح دسترسی را تنظیم کنید و بعد از آزمون، آن را به سطح ایمنتری مثلاً 0755 یا 0750 تغییر دهید.
2. استفاده از کنترل پنل هاست:
وارد کنترل پنل هاست خود (مثل cPanel یا Plesk) شوید.
به مدیریت فایلها (File Manager) بروید.
پوشه مورد نظر را پیدا کنید، روی آن کلیک راست کرده و گزینه "Permissions" را انتخاب کنید.
اینجا میتوانید سطح دسترسی فعلی را بررسی و در صورت لزوم تغییر دهید.
3. استفاده از SSH:
اگر به SSH دسترسی دارید، وارد سرور خود شوید.
دستور زیر را برای بررسی سطح دسترسی پوشه اجرا کنید:
sh Copy code
ls -ld /path/to/your/storage
در خروجی، اعداد و حروف قبل از نام پوشه نشاندهنده سطح دسترسی آن هستند، به شکل drwxrwxrwx.
برای تغییر سطح دسترسی میتوانید دستور زیر را اجرا کنید:
sh Copy code
chmod 0777 /path/to/your/storage
توجه:
همیشه توجه داشته باشید که تنظیمات دسترسی اینچنینی (مثلاً 0777) برای اهداف تست و دیباگ مناسب هستند، اما پس از اینکه مطمئن شدید همه چیز به درستی کار میکند باید سطح دسترسی را به حالت ایمنتری برگردانید تا امنیت سرور شما به خطر نیفتد.
استفاده از سطح دسترسی مناسب از نفوذهای غیرمجاز جلوگیری میکند و به امنیت سرور شما کمک میکند.
Assistant2024-11-19 12:06 UTC Copy response
سطح دسترسی 0755 به این معناست که صاحب پوشه میتواند بخواند، بنویسد و اجرا کند، در حالی که گروه و دیگران فقط میتوانند بخوانند و اجرا کنند. به طور معمول، سطح دسترسی 0755 برای اکثر پوشهها کافی است و امکان نوشتن توسط اسکریپتهای PHP را نمیدهد.
اگر نیاز دارید که اسکریپت PHP شما (مانند upload.php) بتواند در پوشه storage بنویسد، میتوانید به صورت موقت سطح دسترسی را به 0777 تغییر دهید تا امکان نوشتن ایجاد شود، و سپس بعد از اطمینان از عملکرد صحیح، تلاش کنید تا به سطح دسترسی ایمنتری برگردید. برای تغییر سطح دسترسی میتوانید از هر کدام از روشهای توضیح دادهشده استفاده کنید.
تغییر سطح دسترسی با استفاده از FTP:
با استفاده از یک FTP Client مانند FileZilla متصل شوید.
به سرور و دایرکتوری /storage بروید.
روی پوشه storage کلیک راست کرده و گزینه "File Permissions" یا "Permissions" را انتخاب کنید.
سطح دسترسی را به 0777 تغییر داده و اعمال کنید.
تغییر سطح دسترسی با استفاده از SSH:
در صورتی که به SSH دسترسی دارید و با استفاده از دستور زیر سطح دسترسی را تغییر دهید:
sh Copy code
chmod 0777 /path/to/your/storage
تست مجدد
پس از تغییر سطح دسترسی، اسکریپت را مجدداً تست کنید تا ببینید آیا مشکل نوشتن برطرف شده است یا خیر. اگر مشکل حل شد، امکانسنجی کنید که چطور میتوانید مجدداً سطح دسترسی را به حالت ایمنتری نظیر 0755 برگردانید، مثلاً با استفاده از گروهبندی کاربر و کاربر وب سرور (مثل www-data) برای تنظیم مالکیت و دسترسیهای متناسبتر.
ایجاد و نگهداری دسترسیهای ایمن برای کاهش خطرات امنیتی بسیار مهم است، بنابراین پس از تست، سعی کنید به بهترین حالت ممکن که عملکرد را نیز تضمین کند، سطح دسترسی را تنظیم کنید.
Assistant2024-11-19 12:08 UTC Copy response
اگر پس از تغییر سطح دسترسی همچنان با مشکل مواجه هستید، احتمالاً مسئله به کد یا پیکربندی PHP مرتبط است. بیایید چند نکته و مشکل رایج را بررسی کنیم:
نمایش خطاها:
مطمئن شوید که خطاها در PHP به درستی نمایش داده میشوند. افزودن خطوط زیر در ابتدای فایل upload.php میتواند به شما در شناسایی علت خطاهای احتمالی کمک کند:
php Copy code
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
بررسی آدرس URL:
مطمئن شوید که آدرسی که وارد میکنید به درستی تایپ شده و واقعی است. برخی آدرسها ممکن است به شکل نادرست کپی شده باشند یا فایل مورد نظر در دسترس نباشد.
مطالعه لاگهای PHP و سرور:
به لاگهای PHP و سرور (مثل آپاچی یا Nginx) مراجعه کنید. ممکن است اطلاعات و جزئیات بیشتری درباره خطای موجود داشته باشید.
فایل فعالیت PHP environment:
بررسی کنید که تابع file_get_contents برای دانلود از URLها فعال باشد. به تنظیم allow_url_fopen در فایل php.ini توجه کنید:
بررسی کنید که آیا این تنظیم فعال است یا نه (باید On باشد).
در صورت نیاز به تغییر این تنظیم، میتوانید از بخش تنظیمات PHP محیط هاست استفاده کنید، یا اگر دسترسی مستقیم دارید، فایل php.ini را ویرایش کنید:
ini Copy code
allow_url_fopen = On
بررسی مسیر درست اجرای کد:
اطمینان حاصل کنید که تمامی فایلهای PHP در جای درستی قرار گرفتهاند و همهی رفرنسها به توابع و فایلهای دیگر (مثل require_once) صحیح هستند.
بررسی تابع filter_input:
میتوانید اگر اعتبارسنجی URL مشکلساز بود، به سادگی آن را تست کنید:
php Copy code
$fileUrl = $_POST['file_url'];
جایگزین کنید و ببیند خط مشکلی را حل میکند یا نه.
اجازه دسترسی سرور به اینترنت:
مطمئن شوید سرور شما دارای امکان دسترسی به وب (اینترنت) است و فایروال یا تنظیمات دیگری این دسترسی را محدود نکردهاند.
اگر با این تغییرات همچنان مشکل وجود دارد، لطفاً جزئیات بیشتری از خطا یا وضعیت سرور به اشتراک بگذارید تا به شما کمک بیشتری بکنم.
کد جوری درست کن که روی آپلود کلیک کنم یک اسپینر بیاد و همونجا فرایند نشون بده و لینک جدید برام بده تو همون صفحه
Assistant2024-11-19 12:10 UTC Copy response
برای اضافه کردن یک اسپینر لودینگ و نمایش نتیجه آپلود در همان صفحه، میتوانید از ترکیبی از HTML، CSS، JavaScript و PHP استفاده کنید. در اینجا راهکاری را ارائه میدهیم که با استفاده از جاوااسکریپت (AJAX) کار میکند، تا بدون نیاز به بارگذاری مجدد صفحه، نتیجه آپلود را نمایش دهد:
کد کامل:
میتوانید محتوای زیر را در فایل index.php یا upload.php خود جایگزین یا استفاده کنید:
Assistant2024-11-19 12:13 UTC Copy response
متوجهام که میخواهید فایل را از یک URL دانلود کنید و متأسفانه خطای "لینک نامعتبر است" را دریافت میکنید. برای رفع این مشکل، باید برخی موارد را بررسی کنیم و تغییراتی در کد اعمال کنیم تا مطمئن شویم که URL به درستی پردازش میشود.
بیایید کد PHP را بررسی و اصلاح کنیم تا مطمئن شویم مشکل از فیلتر URL نیست. آزمایش با استفاده از روشهای سادهتر برای واکشی دادهها از URL به تشخیص مشکل کمک میکند.
اصلاح کد PHP:
در فایل upload.php مطمئن شوید که بخش مربوط به دریافت URL به صورت زیر اصلاح شده است:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// دریافت URL به روش سادهتر
$fileUrl = $_POST['file_url'];
// بررسی وجود URL
if (empty($fileUrl)) {
die("لینک نامعتبر است.");
}
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// بررسی امکان دانلود فایل
try {
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
throw new Exception("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent) === false) {
throw new Exception("مشکلی در ذخیره فایل به وجود آمد.");
}
echo "فایل ذخیره شد!<br/>";
echo "لینک دانلود: <a href='download.php?file={$fileName}'>دانلود</a>";
} catch (Exception $e) {
die("خطا: " . $e->getMessage());
}
}
توضیحات:
از $_POST['file_url'] مستقیم استفاده کردیم تا بررسی کنیم آیا مشکل از تابع filter_input در آن کامپوننت خاص است یا خیر.
افزودن یک try-catch برای مدیریت بهتر خطاها که خطاها را مدیریت خواهد کرد و به ما کمک خواهد کرد تا بفهمیم مشکل از کجا میتواند باشد.
بهتر است در ابتدا تنظیمات allow_url_fopen در php.ini مطمئن شوید فعال باشد. این تنظیم لازمهی استفاده از file_get_contents برای منابع خارجی است.
پس از اعمال این تغییرات، دوباره امتحان کنید و ببینید آیا مشکل حل شده است یا خیر. اگر هنوز خطا وجود دارد، متن دقیق خطا میتواند به تحلیل مشکل بیشتر کمک کند.
الان شد اما فقط همون فایل نشون میده و تصویر دیگری آپلود نمیشه کاری کن هر تصویر با فرمت png و یا jpg و یا JPEG آپلود بشه
Assistant2024-11-19 12:19 UTC Copy response
برای اطمینان از اینکه فقط فایلهایی با فرمتهای خاص (مانند png، jpg و jpeg) دانلود و ذخیره میشوند، میتوانید بررسی فرمت فایل را اضافه کنید. این بررسی بهطور ساده با استفاده از تابع pathinfo و زنجیرهی شرطهای if برای تطبیق پسوند فایل انجام میشود.
کد بهروزشده:
بخشی از کد مربوط به آپلود فایل را به صورت زیر اصلاح کنید تا بررسی فرمت فایل را انجام دهد:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// دریافت URL به روش سادهتر
$fileUrl = $_POST['file_url'];
// بررسی وجود URL
if (empty($fileUrl)) {
die("لینک نامعتبر است.");
}
// بررسی فرمت فایل
$fileExtension = strtolower(pathinfo(parse_url($fileUrl, PHP_URL_PATH), PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg'];
if (!in_array($fileExtension, $allowedExtensions)) {
die("فقط فایلهای با فرمتهای PNG، JPG و JPEG پشتیبانی میشوند.");
}
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// بررسی امکان دانلود فایل
try {
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
throw new Exception("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent) === false) {
throw new Exception("مشکلی در ذخیره فایل به وجود آمد.");
}
echo "فایل ذخیره شد!<br/>";
echo "لینک دانلود: <a href='download.php?file={$fileName}'>دانلود</a>";
} catch (Exception $e) {
die("خطا: " . $e->getMessage());
}
}
توضیحات:
بررسی فرمت فایل : با استفاده از pathinfo، پسوند فایل استخراج میشود و با استفاده از in_array بررسی میشود که آیا در میان فرمتهای مجاز (png, jpg, jpeg) قرار دارد یا نه.
در صورتی که پسوند فایل نامناسب باشد، پیام خطا نمایش داده میشود و فرآیند دانلود ادامه نخواهد داشت.
همچنان از try-catch برای مدیریت خطاها و احتمال وقوع مشکلات در حین دانلود یا ذخیرهسازی استفاده میشود.
این تنظیم به شما اجازه میدهد تا مطمئن شوید فقط تصاویر با فرمتهای مجاز آپلود میشوند و احتمال دسترسی به فایلهای غیرمجاز را کاهش میدهد.
شد کاری کن تصاویر با فرمت webp هم بشه
Assistant2024-11-19 12:23 UTC Copy response
برای اضافه کردن پشتیبانی از فرمت webp، کافیست که این فرمت را به لیست فرمتهای مجاز اضافه کنیم. کد مربوطه با اضافه کردن webp به لیست فرمتها بهروز میشود:
بهروزرسانی کد:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// دریافت URL به روش سادهتر
$fileUrl = $_POST['file_url'];
// بررسی وجود URL
if (empty($fileUrl)) {
die("لینک نامعتبر است.");
}
// بررسی فرمت فایل
$fileExtension = strtolower(pathinfo(parse_url($fileUrl, PHP_URL_PATH), PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
die("فقط فایلهای با فرمتهای PNG، JPG، JPEG و WEBP پشتیبانی میشوند.");
}
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// بررسی امکان دانلود فایل
try {
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
throw new Exception("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent) === false) {
throw new Exception("مشکلی در ذخیره فایل به وجود آمد.");
}
echo "فایل ذخیره شد!<br/>";
echo "لینک دانلود: <a href='download.php?file={$fileName}'>دانلود</a>";
} catch (Exception $e) {
die("خطا: " . $e->getMessage());
}
}
توضیحات:
در آرایه $allowedExtensions، فرمت webp نیز به دیگر فرمتها اضافه شده است.
با انجام این تغییر، اسکریپت اکنون قادر است تصاویر با فرمت webp را همراه با سایر فرمتهای تعریفشده، دریافت و پردازش کند.
از این پس میتوانید تصاویر با هر یک از فرمتهای png, jpg, jpeg, و webp را آپلود و ذخیره کنید. اگر سوال یا نیاز دیگری دارید، بفرمایید.
عالی شد خیلی ممنون اوکی شد ببین الان من میخواستم داخل یک صفحه اچ تی ام ال من اینکار خودکار انجام بشه خودش ببین توضیح میدم
Show full message · 393 lines Assistant2024-11-19 12:29 UTC Copy response
برای انجام کاری که مد نظر شما است، نیاز به ترکیب عملکرد JavaScript و PHP دارید. به این ترتیب که وقتی کاربر روی دکمه «دانلود تصویر» کلیک میکند، تصویر به صورت برنامهریزیشده به سرور شما آپلود شود و سپس لینک مستقیم از آن ارائه گردد. اضافه کردن دکمهای برای کپی لینک نیز میتواند مفید باشد.
بخشهای اصلی کار شما شامل دو مرحله هستند:
ارسال تصویر از سمت کلاینت (مرورگر کاربر) به سرور.
مدیریت آپلود و ارائه لینک دانلود بر روی سرور.
مرحله ۱: JavaScript برای ارسال تصویر
در اینجا یک نمونه پایهای برای بهروزرسانی قسمت جاوااسکریپت به کمک AJAX برای ارسال تصویر به سرور ارائه میکنم:
html Copy code
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
uploadImageToServer(img.src);
}
});
});
}
function uploadImageToServer(imageUrl) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
fetch('upload.php', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(data => {
if (data.success) {
// نمایش لینک دانلود و دکمه کپی
displayDownloadLink(data.url);
} else {
alert('خطا در آپلود تصویر');
}
});
});
}
function displayDownloadLink(url) {
const link = document.createElement('div');
link.innerHTML = `<p>لینک دانلود: <a href="${url}" target="_blank">${url}</a></p>`;
document.body.appendChild(link);
const copyButton = document.createElement('button');
copyButton.textContent = 'کپی لینک 📋';
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(url).then(() => {
alert('لینک کپی شد');
});
});
document.body.appendChild(copyButton);
}
});
</script>
کد کامل یکجا درست کن بفرست
Assistant2024-11-19 12:32 UTC Copy response
بسیار خوب، من یک راهحل کامل برای شما آماده میکنم که شامل دو بخش است: HTML/JavaScript برای ارسال تصاویر به سرور و PHP برای مدیریت آپلود و ارائه لینک دانلود.
فایل HTML و JavaScript (index.html)
این فایل HTML شامل یک دکمه برای دانلود خواهد بود که با استفاده از AJAX تصویر را به سرور ارسال میکند و سپس لینک دانلود را نمایش میدهد.
من گفتم کد بزرگی که همین پیشتر فرستادم کامل درست کن که فرایندی که گفتم انجام بده
Assistant2024-11-19 12:33 UTC Copy response
از آنجایی که درخواست شما شامل انجام عملیات پیچیدهای از طریق جاوااسکریپت و PHP بود، بیایید کل فرآیند را در قالب یک پروژه با دو فایل اساسی (HTML و PHP) یکپارچه کنیم تا دقیقاً همانی را که دوست دارید داشته باشید. این شامل بارگذاری تصاویر به سرور شما و نمایش لینک دانلود است.
کد کامل HTML با جاوااسکریپت
این کد HTML شامل جاوااسکریپت است که تصاویر را شناسایی کرده و آنها را به سرور برای ذخیرهسازی ارسال میکند.
Show full message · 393 lines Assistant2024-11-19 12:34 UTC Copy response
برای انجام دادن درخواست شما که شامل بارگذاری تصاویر به سرور و نمایش لینکهای دانلود به صورت مستقیم در وبپیج میباشد، نیاز به تغییراتی در کد جاوااسکریپت موجود و همچنین افزودن PHP مناسب داریم. به این ترتیب:
کد HTML همراه با جاوااسکریپت
سعی کردهام همه ویژگیهای درخواستی شما را در اینجا بگنجانم، با امکان آپلود تصاویر به سرور و نمایش لینک دانلود:
کد منو دارم میگم کامل بفرست مخفی سازی داشت و نوار پیشرفت داشت کد کامل کامل
Assistant2024-11-19 12:37 UTC Copy response
متاسفم برای ناهماهنگی پیشین. بیایید تمامی امکاناتی که ذکر کردهاید (مانند مخفیسازی، نمایش نوار پیشرفت و بارگذاری با PHP) را به صورت کامل در کد ترکیب کنیم.
ترکیب کد JavaScript و HTML:
این کد شامل مخفیسازی، نمایش نوار پیشرفت و آپلود به سرور است.
Show full message · 224 lines چیکار کردی سایت گرادیو باز نمیشه مثل قبل باشه همه چی فقط لینک دانلود فرق کنه
Assistant2024-11-19 12:39 UTC Copy response
ممکن است که برخی از تغییراتی که اعمال کردم با ساختار فعلی وبسایت Gradio و نحوه استفاده شما سازگار نباشند. بیایید به کد اصلی شما بازگردیم و تنها بخشهایی را که مربوط به بارگذاری تصاویر و ارائه لینکهای دانلود هستند تغییر دهیم بدون اینکه سایر عملکردهای نوار پیشرفت و سایر عناصر صفحه جا به جا شوند.
کد HTML و JavaScript:
در این کد، وقتی بر روی دکمه دانلود کلیک میکنید، تصاویر در صفحه شناسایی و به سرور ارسال میشوند و لینکهای دانلود مناسب ارائه میشود.
Show full message · 245 lines Show full message · 393 lines Assistant2024-11-19 12:43 UTC Copy response
مطمئنم که با ترکیب کدهای جاوااسکریپت و PHP، میتوانیم به نیازهای شما پاسخ دهیم: یعنی زمانی که دکمه "تایید" برای ذخیره تصویر کلیک میشود، تصویر به سرور شما آپلود میشود و لینک مستقیم دانلود برای آن ارائه میگردد. همچنین میتوانیم یک دکمه برای کپی کردن لینک در نظر بگیریم.
کد HTML و JavaScript:
این کد شامل تغییراتی است که عکس را آپلود کرده و پس از آپلود، لینک دانلود را نشان میدهد.
Show full message · 472 lines روی ذخیره تصویر که کلیک میکنم میکنه در حال آپلود تصویر خب اما لینک نشون نمیده در اعلان جدید چرا
Assistant2024-11-19 12:46 UTC Copy response
برای رفع مشکل و اطمینان از اینکه لینک دانلود پس از آپلود به درستی نمایش داده میشود، لازم است چند مرحله را بررسی کنیم و مطمئن شویم که مسیرها و اسکریپت به درستی کار میکنند. این مشکل ممکن است به تنظیمات سرور، اتصال به اینترنت و یا کدنویسی مربوط باشد. در زیر کدها را بررسی و اصلاح میکنیم:
HTML و JavaScript
در اینجا باید مطمئن شویم که درخواست به درستی ارسال میشود و پاسخ سرور به خوبی مدیریت میشود.
بگو چی بدم تا کامل کنی کد
Assistant2024-11-19 12:47 UTC Copy response
برای اینکه بتوانم به شما دقیقتر کمک کنم، لطفاً اطلاعات زیر را ارائه دهید تا درک بهتری از مشکلات توافقی و چگونگی حل آنها داشتم باشم:
خطای دقیق یا پیغام خطا : آیا خطایی دریافت میکنید؟ اگر نه، چه اتفاقی (یا اتفاقی نمیافتد) که انتظار داشتید رخ دهد؟
تنظیمات محیط و سرور :
آیا PHP روی سرور فعال است؟
آیا تنظیمات آپلود فایل (مثل upload_max_filesize و post_max_size) در php.ini به اندازه کافی بزرگ است؟
آیا allow_url_fopen در php.ini فعال است؟
منبع دقیق تصاویر : اگر تصاویر از منابع خارجی بارگذاری میشوند، ممکن است به خاطر مسائل CORS یا دسترسپذیری بلاک شوند.
دامنه دقیق (در صورت عمومی بودن) : این به من کمک میکند تا مطمئن شوم که آدرسها/URLهای استفاده شده صحیح هستند.
هر گونه تغییر که قبلاً در کد اعمال کردهاید : این به درک بهتر از اینکه چه تغییراتی ممکن است اثر گذاشته باشند کمک میکند.
با ارائه این اطلاعات، مطمئن میشوم که کد نهایی به درستی عمل کند و نیازهای شما را برآورده کند. اگر نیاز به راهنمایی درباره نحوه بررسی یک این موارد دارید، بفرمایید.
Show full message · 420 lines Assistant2024-11-19 12:58 UTC Copy response
اگر پس از کلیک روی دکمه "ذخیره تصویر" لینک جدید به درستی نمایش داده نمیشود، ممکن است مشکل از پاسخ سرور PHP یا نحوه دریافت و مدیریت این پاسخ در جاوااسکریپت باشد. بیایید چند گام برای رفع مشکل برداریم:
بررسی کد PHP (upload.php) : مطمئن شوید که فایل PHP تصویر را به درستی ذخیره کرده و URL درست را باز میگرداند.
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
بررسی کنسول مرورگر : آیا خطایی در کنسول مرورگر ظاهر میشود؟ همچنین پاسخ network از درخواست AJAX را بررسی کنید تا مطمئن شوید پاسخی که از سرور میآید مطابق انتظار است.
بررسی کد جاوااسکریپت : اطمینان حاصل کنید که جاوااسکریپت پاسخ مناسب را دریافت میکند و نمایش میدهد.
آیا پاسخ به صورت JSON از طریق PHP دریافت میشود؟ در کد جاوااسکریپت، بهطور خاص بخش .then(response => response.json()) و پس از آن .then(data => {...}) را بررسی و اصلاح کنید:
کد جاوااسکریپت
مطمئن شوید که در جاوااسکریپت همهچیز به درستی ارسال و دریافت میشود:
ببین کد کامل یکجا دوباره درست کن بدون هیچ مشکلی که روی ذخیره میزنم داخل یک اعلان مثل اسپینر بچرخه آپلود شد لینک مستقیم بده با یک دکمه کپی لینک داخل اعلان
Assistant2024-11-19 13:00 UTC Copy response
بسیار خوب! بیایید کل سیستم را از نو با یکدیگر بسازیم و اطمینان حاصل کنیم که هر بخشی به خوبی کار میکند. این شامل دو بخش اصلی خواهد بود: HTML/JavaScript و PHP برای آپلود و دریافت لینک.
کد HTML و JavaScript
اینجا کد کامل HTML و JavaScript را آوردیم که شامل کلیه امکانات مد نظر شماست:
Assistant2024-11-19 13:02 UTC Copy response
برای پیادهسازی کامل پروژهای که به دنبال آن هستید، کد HTML/JavaScript و PHP را با در نظر گرفتن مواردی که ارائه کردهاید، آماده میکنم. دامنهای که ارائه دادید را نیز برای لینکدهی مستقیم تصاویر آپلود شده، پیادهسازی خواهم کرد.
کد HTML و JavaScript
این کد باید در فایل index.php قرار بگیرد:
کد کامل بده مخفی سازی داشت متن داشت کد اول ببین دوباره
Show full message · 393 lines Assistant2024-11-19 13:05 UTC Copy response
برای انجام کاری که توضیح دادید، این کد HTML و JavaScript را به صورت کامل پیادهسازی میکنیم که شامل بخشی است که تصاویر را پس از ذخیره با یک اعلان و امکان کپی لینک دانلود نمایش میدهد. در این پیادهسازی، پس از انتخاب و ذخیره تصویر، تصویر به سرور آپلود میشود و لینک دانلود آن نمایش داده میشود.
کد کامل HTML و JavaScript
Show full message · 300 lines کد کامل بده دهنم سرویس شد
Assistant2024-11-19 13:07 UTC Copy response
متاسفم که تا حالا به نتیجه نرسیدیم. بیایید یک بار دیگر کل کد را به صورت یکجا ایجاد کنیم، تا مطمئن شویم که همه چیز به درستی کار میکند. کل این فرآیند شامل HTML/JavaScript برای نمایش صفحه و تعامل کاربر، و PHP برای مدیریت آپلود فایل خواهد بود.
کد HTML و JavaScript
در اینجا کد را به طور کامل در یک فایل HTML قرار میدهیم:
Show full message · 287 lines Show full message · 405 lines Assistant2024-11-19 13:30 UTC Copy response
بله، میتوانید کدی را که در HTML شما وجود دارد بهگونهای طراحی کنید که به سرور خاص شما متصل شود و بتواند لینکها را با دامنه شما ایجاد کند. این کار شامل این است که مطمئن شوید کد جاوااسکریپت به فایل PHP درست بر روی سرور شما متصل است و فایل PHP به درستی لینکها را با دامنه شما ساختاردهی و بازگرداند.
ساختار کلی کار:
کد HTML و JavaScript : این کد را میتوانید در هر صفحه HTML قرار دهید. مطمئن باشید که فایل upload.php بر روی سرور شما و در لینک دلخواه دسترسپذیر است و جاوااسکریپت با استفاده از AJAX به آن متصل میشود.
کد PHP : این کد روی سرور شما قرار داده میشود تا فایلها را دریافت کند و لینک جدید تولید نماید.
نحوه انجام کار:
کد اصلی HTML را به همان شکلی که هست، در فایل HTML خود یا هر صفحهای که مایلید قرار دهید. کد جاوااسکریپت داخل این صفحه باید با فایل PHP شما بر روی سرور از طریق AJAX تعامل داشته باشد.
کد HTML و JavaScript برای استفاده عمومی
عالی این یکی کد منم درست کن که دکمه ذخیره تصویر داشته باشه مثل اون
Show full message · 393 lines Assistant2024-11-19 13:33 UTC Copy response
بیایید کد شما را به گونهای اصلاح کنیم که وقتی تصویر را ذخیره کردید، تصویر به سرور شما ارسال شود و لینک جدید با دامنه شما ایجاد شود. این شامل بخش HTML و JavaScript برای تعامل کاربر و PHP برای پردازش آپلود مدل میباشد.
کد HTML و JavaScript
در ابتدا، مطمئن شوید که کد زیر را به درستی در فایل HTML خود قرار دادهاید:
Show full message · 288 lines این کد کامل بده همه چی باشه حتی مخفی سازی ها و نمایش متن ها همه چی
Assistant2024-11-19 13:35 UTC Copy response
بسیار خوب، اینجا کد کامل HTML و JavaScript همراه با PHP برای آپلود تصاویر به سرور و نمایش لینک دانلود به شما ارائه میشود. این کد شامل عناصر مخفیسازی و نمایش متنی که شما درخواست کردید نیز خواهد بود.
کد HTML و JavaScript
این کد را در فایل index.html (یا هر صفحه HTML دیگر) خود قرار دهید:
Show full message · 288 lines Show full message · 393 lines Assistant2024-11-19 13:38 UTC Copy response
بیایید یک بار دیگر کل سیستم را به صورت کامل بازنویسی کنیم، با اطمینان از اینکه همه خواستههای شما را شامل شود. این شامل HTML, CSS و JavaScript برای تعامل با کاربر و PHP برای مدیریت آپلود و نمایش لینک خواهد بود.
کد HTML و JavaScript
این کد را در فایل HTML خود قرار دهید. این شامل عنصرهای رابط کاربری و منطق لازمه برای آپلود تصویر به سرور شما است.
Show full message · 458 lines میشه این دوتا کد با هم مخلوط کنی نیاز نباشه جدا جدا داخل فایل ها قرار بدیم
Assistant2024-11-19 14:10 UTC Copy response
مطمئناً! میتوانید کد HTML و JavaScript را همراه با بخشی از کد PHP در یک فایل PHP قرار دهید. این به شما امکان میدهد همه چیز را در یک فایل مدیریت کنید. برای این کار باید کد PHP را در بین برچسبهای <?php ?> قرار دهید. اینجا یک نمونهی کامل از چگونگی انجام این کار برای شما فراهم شدهاست:
کد ترکیبی HTML، JavaScript و PHP
Show full message · 457 lines Assistant2024-11-19 14:12 UTC Copy response
برای استفاده از HTML و JavaScript شما به PHP برای پردازش فایلها نیاز دارید. اما میتوانید بخشهای مربوط به HTML و جاوااسکریپت را در یک فایل .html قرار دهید و سپس از یک فایل PHP برای پردازش آپلود استفاده کنید. در اینجا چگونگی انجام این کار با دو فایل جداگانه توضیح داده میشود:
۱. فایل HTML (index.html)
این فایل شامل ساختار HTML و منطق جاوااسکریپت است و نمایش UI را انجام میدهد:
Show full message · 364 lines به من بگو میشه هر دو کد در یک کد اچ تی ام ال قرار بدیم
Assistant2024-11-19 14:14 UTC Copy response
بسیاری از سرورها قادر به اجرای کدهای PHP در صفحات HTML نیستند زیرا HTML بهطور پیشفرض برای نمایش محتوای استاتیک طراحی شده است. با این حال، میتوان یک فایل PHP ساخت که شامل کد HTML و PHP ترکیب شده باشد. بنابراین، برای اجرای کدهای PHP بهدرستی، باید فایل شما با پسوند .php ذخیره شود.
در اینجا نمونهای از چگونگی ترکیب کد HTML و PHP در یک فایل PHP ارائه شده است:
ترکیب کد HTML و PHP در یک فایل .php
Show full message · 359 lines فقط کوتاه جواب بده داخل کد اچ تی ام ال میشه یا نه
Assistant2024-11-19 14:15 UTC Copy response
نه، کد PHP نمیتواند به عنوان بخشی از یک فایل HTML به تنهایی عمل کند. فایل باید با پسوند .php ذخیره شود تا کد PHP اجرا شود.
Not yet included in a published release.
′
])
;
s t o r a g e P a t h . storagePath . s t or a g e P a t h . code Copy code
if (file_exists($filePath)) {
// تنظیم هدرهای دانلود
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
function deleteOldFiles(p a t h , path, p a t h , maxAgeInSeconds = 1800) {
f i l e s = g l o b ( files = glob( f i l es = g l o b ( path . '*');
foreach (f i l e s a s files as f i l es a s file) {
if (is_file(file) && time() - filemtime( file) > maxAgeInSeconds) {
unlink( file);
}
}
} و یک فایل با نام functions.php با این کد <?php
// تابع حذف فایلهای قدیمی
function deleteOldFiles(p a t h , path, p a t h , maxAgeInSeconds = 1800) {
if (!is_dir(path)) {
die("مسیر path معتبر نیست یا وجود ندارد.");
}
code Copy code
$files = glob($path . '*'); // دریافت تمام فایلها در پوشه
foreach ($files as $file) {
if (is_file($file)) {
$fileTime = filemtime($file); // زمان آخرین تغییر فایل
$currentTime = time(); // زمان فعلی
$age = $currentTime - $fileTime;
if ($age > $maxAgeInSeconds) {
unlink($file); // حذف فایل
}
}
}
} و یک فایل با نام index.php اینم کدش
code Copy code
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
if (_SERVER['REQUEST_METHOD'] === 'POST') {
// حذف فایلهای قدیمی
deleteOldFiles( storagePath);
code Copy code
$fileUrl = filter_input(INPUT_POST, 'file_url', FILTER_VALIDATE_URL);
if (!$fileUrl) {
die("لینک نامعتبر است.");
}
// نام فایل تصادفی برای ذخیره
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// دانلود فایل
if (file_put_contents($filePath, file_get_contents($fileUrl))) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='download.php?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
function deleteOldFiles(p a t h , path, p a t h , maxAgeInSeconds = 1800) {
f i l e s = g l o b ( files = glob( f i l es = g l o b ( path . '*');
foreach (f i l e s a s files as f i l es a s file) {
if (is_file(file) && time() - filemtime( file) > maxAgeInSeconds) {
unlink( file);
}
}
}
مطمئن شوید که پوشهای به نام storage در همان مسیری که فایل upload.php قرار دارد وجود دارد و دسترسی نوشتن به آن داده شده است.
اگر فایل را آپلود کردید و صفحهای با خطا مواجه شدید، لطفاً بررسی کنید خطاهای مورد نظر در کد برای شما نمایش داده میشود یا خیر.
این کد اصولی را که برای کاربرد شما نیاز است پوشش میدهد: دریافت یک URL از کاربر، دانلود فایل، ذخیره فایل در سرور و در نهایت ایجاد لینک دانلود برای فایل ذخیره شده. اگر مشکل یا سوال دیگری دارید، لطفاً بگویید.
مطمئن شوید که پوشه storage در همان مسیری که فایل upload.php وجود دارد، ایجاد شده و دسترسی نوشتن به آن داده شده است.
این اسکریپت کلیه مراحل شامل ورود URL فایل، دانلود فایل، ذخیرهسازی آن در سرور و ارائه لینک دانلود را انجام میدهد. هیچ محدودیتی برای نوع فایلها وجود ندارد و تمامی فایلها با موفقیت دانلود و ذخیره میشوند مگر اینکه خطای دیگری رخ دهد. اگر مشکلی دارید، لطفاً آن را با من مطرح کنید.
توضیحات:
این کد یک فرم ساده برای وارد کردن URL و دانلود فایل از آن دارد.
هر فایل با یک نام منحصر به فرد و تصادفی ذخیره میشود.
فایلهای قدیمی که بیش از ۳۰ دقیقه از ایجاد آنها گذشته باشد حذف میشوند.
لینک دانلود برای هر فایل ذخیره شده ارائه میشود.
اطمینان حاصل کنید که پوشه storage قبلاً ایجاد شده و دارای مجوزهای نوشتن است. اگر کماکان مشکل دارید، لطفاً جزئیات بیشتری از پیام خطا را ارائه دهید.
<?php
// مسیر پوشه ذخیرهسازی
$storagePath = __DIR__ . '/storage/';
$maxAgeInSeconds = 1800; // 30 دقیقه
// اطمینان از ایجاد پوشه
if (!is_dir($storagePath)) {
if (!mkdir($storagePath, 0777, true)) {
die("عدم امکان ایجاد پوشه ذخیرهسازی. لطفاً سطح دسترسیها را بررسی کنید.");
}
}
// تابع برای حذف فایلهای قدیمی
function deleteOldFiles($path, $maxAgeInSeconds) {
$files = glob($path . '*');
foreach ($files as $file) {
if (is_file($file) && time() - filemtime($file) > $maxAgeInSeconds) {
unlink($file);
}
}
}
// حذف فایلهای قدیمی
deleteOldFiles($storagePath, $maxAgeInSeconds);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = $_POST['file_url'];
// بررسی URL
if (!filter_var($fileUrl, FILTER_VALIDATE_URL)) {
die("لینک نامعتبر است.");
}
// نام فایل
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// تلاش برای دانلود فایل
set_error_handler(function($severity, $message, $file, $line) {
throw new ErrorException($message, $severity, $severity, $file, $line);
});
try {
$fileContent = file_get_contents($fileUrl);
if ($fileContent === false) {
throw new Exception("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent) === false) {
throw new Exception("مشکلی در ذخیره فایل به وجود آمد.");
}
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='{$_SERVER['PHP_SELF']}?file={$fileName}'>دانلود</a>";
} catch (Exception $e) {
die("خطا: " . $e->getMessage());
} finally {
restore_error_handler();
}
}
if (isset($_GET['file'])) {
$fileName = basename($_GET['file']);
$filePath = $storagePath . $fileName;
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>آپلود از لینک</title>
<style>
html, body {
text-align: right;
direction: rtl;
}
</style>
</head>
<body>
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
</body>
</html>
راهنمایی بیشتر: اگر همچنان با مشکل روبرو هستید:
مطمئن شوید که سرور شما دارای دسترسی به اینترنت است.
تنظیمات php.ini را بررسی کنید و مطمئن شوید allow_url_fopen روی On قرار دارد. این تنظیم برای اجازه دسترسی به فایلهای منابع خارجی استفاده میشود، مانند دسترسی با file_get_contents.
آزمایش کنید که آیا میتوانید دستی از همان URL فایل را دانلود کنید (برای مثال، با استفاده از یک مرورگر).
اگر این تغییرات مشکل را حل نمیکند، اشکال بیشتر میتواند به ویژه به خطاهایی که هنگام اجرای کد مشاهده میکنید بستگی داشته باشد.
′
])
;
s t o r a g e P a t h . storagePath . s t or a g e P a t h . code Copy code
if (file_exists($filePath)) {
// تنظیم هدرهای دانلود
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
die("فایل یافت نشد.");
}
function deleteOldFiles(p a t h , path, p a t h , maxAgeInSeconds = 1800) {
f i l e s = g l o b ( files = glob( f i l es = g l o b ( path . '*');
foreach (f i l e s a s files as f i l es a s file) {
if (is_file(file) && time() - filemtime( file) > maxAgeInSeconds) {
unlink( file);
}
}
} و یک فایل با نام functions.php با این کد <?php
// تابع حذف فایلهای قدیمی
function deleteOldFiles(p a t h , path, p a t h , maxAgeInSeconds = 1800) {
if (!is_dir(path)) {
die("مسیر path معتبر نیست یا وجود ندارد.");
}
code Copy code
$files = glob($path . '*'); // دریافت تمام فایلها در پوشه
foreach ($files as $file) {
if (is_file($file)) {
$fileTime = filemtime($file); // زمان آخرین تغییر فایل
$currentTime = time(); // زمان فعلی
$age = $currentTime - $fileTime;
if ($age > $maxAgeInSeconds) {
unlink($file); // حذف فایل
}
}
}
} و یک فایل با نام index.php اینم کدش
code Copy code
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
if (_SERVER['REQUEST_METHOD'] === 'POST') {
// حذف فایلهای قدیمی
deleteOldFiles( storagePath);
code Copy code
$fileUrl = filter_input(INPUT_POST, 'file_url', FILTER_VALIDATE_URL);
if (!$fileUrl) {
die("لینک نامعتبر است.");
}
// نام فایل تصادفی برای ذخیره
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// دانلود فایل
if (file_put_contents($filePath, file_get_contents($fileUrl))) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='download.php?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
function deleteOldFiles(p a t h , path, p a t h , maxAgeInSeconds = 1800) {
f i l e s = g l o b ( files = glob( f i l es = g l o b ( path . '*');
foreach (f i l e s a s files as f i l es a s file) {
if (is_file(file) && time() - filemtime( file) > maxAgeInSeconds) {
unlink( file);
}
}
}
index.php php Copy code
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>آپلود از لینک</title>
<style>
html, body {
text-align: right;
direction: rtl;
}
</style>
</head>
<body>
<?php
require_once 'functions.php';
// مسیر پوشه ذخیرهسازی
$storagePath = __DIR__ . '/storage/';
deleteOldFiles($storagePath);
?>
<form action="upload.php" method="POST">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
</body>
</html>
upload.php php Copy code
<?php
require_once 'functions.php';
$storagePath = __DIR__ . '/storage/';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// حذف فایلهای قدیمی
deleteOldFiles($storagePath);
$fileUrl = filter_input(INPUT_POST, 'file_url', FILTER_VALIDATE_URL);
if (!$fileUrl) {
die("لینک نامعتبر است.");
}
// نام فایل تصادفی برای ذخیره
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
// دانلود فایل
if (file_put_contents($filePath, file_get_contents($fileUrl))) {
echo "فایل ذخیره شد!<br>";
echo "لینک دانلود غیرمستقیم: <a href='download.php?file={$fileName}'>دانلود</a>";
} else {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
}
// تابع برای حذف فایلهای قدیمی
function deleteOldFiles($path, $maxAgeInSeconds = 1800) {
$files = glob($path . '*');
foreach ($files as $file) {
if (is_file($file) && time() - filemtime($file) > $maxAgeInSeconds) {
unlink($file);
}
}
}
نکات:
مطمئن شوید که پوشه storage وجود دارد و دارای سطح دسترسی نوشتن برای سرور شما است.
همگی فایلها باید در یک پوشه قرار داشته باشند و بررسی کنید که تمامی فراخوانیهای require و require_once صحیح باشد.
با بهروزرسانی خطاهای احتمالی، مطمئن شوید که allow_url_fopen فعال است. این تنظیمات در فایل php.ini موجود است و برای دریافت فایلها از URLها مورد نیاز است.
نکات دیگر:
هنگام دیباگ کردن، مطمئن شوید که تمام پیامهای خطای PHP بهدرستی تنظیم شدهاند تا نمایش داده شوند، که میتواند با تنظیمات ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); در ابتدای کد شما انجام شود.
مطمئن شوید که سرور شما از سطح دسترسی مناسب برخوردار است و تمامی ابزارهای مورد نیاز نظیر file_get_contents در دسترس است.
لطفاً بررسیها را انجام دهید و اگر همچنان با مشکلی مواجه هستید، اطلاعات بیشتری درباره جزئیات خطا ارائه دهید تا بهتر بتوانم به شما کمک کنم.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>آپلود از لینک</title>
<style>
html, body {
text-align: right;
direction: rtl;
}
.spinner {
display: none;
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-left-color: #09f;
position: absolute;
top: 50%;
left: 50%;
margin: -18px 0 0 -18px;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.result {
margin-top: 20px;
font-size: 14px;
color: green;
}
</style>
<script>
function uploadFile(event) {
event.preventDefault();
var spinner = document.getElementById('spinner');
var resultDiv = document.getElementById('result');
var fileUrl = document.getElementById('file_url').value;
spinner.style.display = 'block';
resultDiv.innerHTML = '';
var xhr = new XMLHttpRequest();
xhr.open("POST", "upload.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
spinner.style.display = 'none';
resultDiv.innerHTML = xhr.responseText;
}
};
xhr.send("file_url=" + encodeURIComponent(fileUrl));
}
</script>
</head>
<body>
<form onsubmit="uploadFile(event)">
<label for="file_url">لینک فایل:</label>
<input type="url" name="file_url" id="file_url" required>
<button type="submit">آپلود</button>
</form>
<div id="spinner" class="spinner"></div>
<div id="result" class="result"></div>
</body>
</html>
<?php
// این بخش باید در فایل upload.php قرار بگیرد
// تنظیمات و مسیرها
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileUrl = filter_input(INPUT_POST, 'file_url', FILTER_VALIDATE_URL);
if (!$fileUrl) {
die("لینک نامعتبر است.");
}
$fileName = uniqid() . '-' . basename(parse_url($fileUrl, PHP_URL_PATH));
$filePath = $storagePath . $fileName;
$fileContent = @file_get_contents($fileUrl);
if ($fileContent === false) {
die("خطایی در دانلود فایل وجود دارد.");
}
if (file_put_contents($filePath, $fileContent) === false) {
die("مشکلی در ذخیره فایل به وجود آمد.");
}
echo "فایل ذخیره شد!<br/>";
echo "لینک دانلود: <a href='download.php?file={$fileName}'>دانلود</a>";
}
?>
توضیحات:
این کد از تکنولوژی AJAX برای ارسال درخواست به سرور بدون بارگذاری مجدد صفحه استفاده میکند.
JavaScript برای افزودن اسپینر زمانی که فرایند آپلود در حال انجام است استفاده میشود و بعد از انجام فرایند، نتیجه در جای مشخص شده (#result) به نمایش درمیآید.
نتیجه فرایند آپلود به صورت AJAX از upload.php برمیگردد و در صفحه نمایش داده میشود.
تنظیمات CSS برای اسپینر ارائه شده و میتوانید به دلخواه آنها را تغییر دهید.
این روش کاربرپسند است و فعالیتها را به صورت پویا در صفحه نمایش میدهد. اطمینان حاصل کنید که فایل upload.php به درستی در مسیری که باید قرار دارد تا AJAX بتواند به آن دسترسی داشته باشد.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
});
const confirmButton = document.getElementById('confirmButton');
confirmButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const fullscreenImage = document.getElementById('fullscreenImage');
fullscreenImage.src = modalImage.src;
fullscreenOverlay.style.display = 'flex';
document.getElementById('saveNotification').style.display = 'none';
});
const closeFullscreenButton = document.getElementById('closeFullscreenButton');
closeFullscreenButton.addEventListener('click', function() {
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
fullscreenOverlay.style.display = 'none';
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
مرحله ۲: PHP برای مدیریت آپلود ایجاد یا بهروزرسانی فایل upload.php برای ذخیرهسازی تصویر و ارسال لینکی به کلاینت:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
در قسمت جاوااسکریپت، تصویر با کمک fetch به سرور ارسال میشود.
زمانی که تصویر به سرور آپلود شود، PHP پاسخ JSON شامل لینک تصویر آپلود شده را برمیگرداند.
با استفاده از AJAX (به کمک fetch در JS) دریافت لینک انجام میشود و در صفحه نمایش داده میشود. همچنین دکمهای برای کپی کردن لینک نیز اضافه شده است.
فایل مربوط به PHP باید در مکانی قرار گیرد که قابل دسترسی باشد، و دامنه واقعی شما باید جای http://yourdomain.com/ قرار گیرد.
لطفاً اطمینان حاصل کنید که محیط سرور شما از ارسال و دریافت درخواستهای HTTP (CORS) و آپلود فایلها به درستی پشتیبانی میکند.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>دانلود و آپلود تصویر</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
}
#downloadButton {
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
.link-container {
margin: 20px;
font-size: 14px;
color: green;
}
button {
margin-top: 10px;
}
</style>
</head>
<body>
<button id="downloadButton">دانلود تصاویر 📥</button>
<div id="linksContainer" class="link-container"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const linksContainer = document.getElementById('linksContainer');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('img[src$=".webp"], img[src$=".png"], img[src$=".jpg"], img[src$=".jpeg"]');
images.forEach(img => {
uploadImageToServer(img.src);
});
});
function uploadImageToServer(imageUrl) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayDownloadLink(data.url);
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
});
}
function displayDownloadLink(url) {
const linkElem = document.createElement('div');
linkElem.innerHTML = `<p>لینک دانلود: <a href="${url}" target="_blank">${url}</a></p>`;
const copyButton = document.createElement('button');
copyButton.textContent = 'کپی لینک 📋';
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(url).then(() => {
alert('لینک کپی شد');
});
});
linkElem.appendChild(copyButton);
linksContainer.appendChild(linkElem);
}
});
</script>
</body>
</html>
فایل PHP (upload.php) این فایل PHP تصویر دریافتی را ذخیره کرده و لینک دانلود آن را برمیگرداند.
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
// ایجاد پوشه ذخیرهسازی در صورت عدم وجود
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
HTML و JavaScript : در این فایل دکمهای برای دانلود تصاویر وجود دارد که تصاویری با فرمتهای مشخصشده در صفحه را شناسایی و ارسال میکند. هر تصویر در سرور آپلود شده و لینک آن نمایش داده میشود.
PHP : اسکریپت PHP که تصویر دریافتی را در پوشه storage ذخیره میکند و لینک مستقیم آن را به صورت JSON برمیگرداند.
نکات:
مسیر درست دامنه : در فایل upload.php، yourdomain.com را با دامنه واقعی خود جایگزین کنید.
تنظیمات امنیتی و دسترسی : اطمینان حاصل کنید که همه چیز بهدرستی در هاست شما تنظیم شده و مجوزهای مناسب برای پوشهها قرار داده شده است.
CORS : ممکن است لازم باشد تنظیمات مربوط به CORS را در سرور خود بررسی کنید تا از دسترسی صحیح محتواها توسط مرورگر مطمئن شوید.
این پیادهسازی باید به شما کمک کند تا تصاویر موجود در صفحه را با فرمتهای مشخص، مستقیماً در سرور خود آپلود کنید و لینکهایشان را به کاربر نشان دهید.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
#downloadButton {
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.link-container {
margin: 20px;
font-size: 14px;
color: green;
}
button {
margin-top: 10px;
}
</style>
</head>
<body>
<button id="downloadButton">دانلود تصاویر 📥</button>
<div id="linksContainer" class="link-container"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const linksContainer = document.getElementById('linksContainer');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('img[src$=".webp"], img[src$=".png"], img[src$=".jpg"], img[src$=".jpeg"]');
images.forEach(img => {
uploadImageToServer(img.src);
});
});
function uploadImageToServer(imageUrl) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayDownloadLink(data.url);
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
});
}
function displayDownloadLink(url) {
const linkElem = document.createElement('div');
linkElem.innerHTML = `<p>لینک دانلود: <a href="${url}" target="_blank">${url}</a></p>`;
const copyButton = document.createElement('button');
copyButton.textContent = 'کپی لینک 📋';
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(url).then(() => {
alert('لینک کپی شد');
});
});
linkElem.appendChild(copyButton);
linksContainer.appendChild(linkElem);
}
});
</script>
</body>
</html>
کد کامل PHP (upload.php) این کد PHP تصویر دریافتی را ذخیره میکند و یک لینک مستقیم به تصویر آپلود شده ارائه میدهد.
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
// ایجاد پوشه ذخیرهسازی در صورت عدم وجود
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
HTML و JavaScript : دکمه "دانلود تصاویر" کلیک میشود که تصاویر روی صفحه (با فرمتهای webp, png, jpg, jpeg) به سرور ارسال شوند.
PHP : تصویر دریافتی را در پوشه storage ذخیره کرده و لینک مستقیم آن را به صورت JSON برمیگرداند.
اطمینان حاصل کنید که yourdomain.com را به دامنه واقعی خود تغییر دهید، و اینکه پوشه storage دارای سطح دسترسی مناسبی برای نوشتن فایلها است.
با این تنظیمات، شما قادر خواهید بود تا تصاویر موجود در صفحه را به صورت مستقیم در سرور خود آپلود و لینک دانلود آنها را به کاربران ارائه کنید.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
});
const confirmButton = document.getElementById('confirmButton');
confirmButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const fullscreenImage = document.getElementById('fullscreenImage');
fullscreenImage.src = modalImage.src;
fullscreenOverlay.style.display = 'flex';
document.getElementById('saveNotification').style.display = 'none';
});
const closeFullscreenButton = document.getElementById('closeFullscreenButton');
closeFullscreenButton.addEventListener('click', function() {
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
fullscreenOverlay.style.display = 'none';
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
}
#downloadButton {
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.link-container {
margin: 20px;
font-size: 14px;
color: green;
}
button {
margin-top: 10px;
}
</style>
</head>
<body>
<button id="downloadButton">دانلود تصاویر 📥</button>
<div id="linksContainer" class="link-container"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const linksContainer = document.getElementById('linksContainer');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('img[src$=".webp"], img[src$=".png"], img[src$=".jpg"], img[src$=".jpeg"]');
images.forEach(img => {
uploadImageToServer(img.src);
});
});
function uploadImageToServer(imageUrl) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayDownloadLink(data.url);
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
});
}
function displayDownloadLink(url) {
const linkElem = document.createElement('div');
linkElem.innerHTML = `<p>لینک دانلود: <a href="${url}" target="_blank">${url}</a></p>`;
const copyButton = document.createElement('button');
copyButton.textContent = 'کپی لینک 📋';
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(url).then(() => {
alert('لینک کپی شد');
});
});
linkElem.appendChild(copyButton);
linksContainer.appendChild(linkElem);
}
});
</script>
</body>
</html>
کد PHP (upload.php) برای بارگذاری تصویر و ارسال لینک دانلود:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
// ساخت پوشه ذخیرهسازی در صورت عدم وجود
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
دکمه دانلود: دکمهای که با کلیک بر روی آن، تمام تصاویر موجود در صفحه با فرمتهای مشخصشده را بارگذاری میکند.
آژاکس و جاوااسکریپت: برای ارسال هر تصویر به سرور استفاده شده است و لینک دانلود مستقیم به کاربر نشان داده میشود.
PHP: برای مدیریت آپلود تصویر و ایجاد URL برای تصاویر مورد استفاده قرار میگیرد.
تنظیمات ضروری: اطمینان حاصل کنید که yourdomain.com به دامنه واقعی شما تغییر داده شود و همچنین پوشه storage با سطح دسترسی مناسب تنظیم شده باشد.
با این روش، هر تصویری که بر روی صفحه وب شما قرار دارد، میتواند به سرور آپلود شود و لینک مستقیم دانلودش به کاربران ارائه شود.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
#downloadButton {
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.link-container {
margin: 20px;
font-size: 14px;
color: green;
}
button {
margin-top: 10px;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
</div>
<button id="downloadButton">دانلود تصاویر 📥</button>
<div id="linksContainer" class="link-container"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const loader = document.getElementById('loader');
const progressBar = document.getElementById('bar');
loader.style.display = 'none';
const downloadButton = document.getElementById('downloadButton');
const linksContainer = document.getElementById('linksContainer');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('img[src$=".webp"], img[src$=".png"], img[src$=".jpg"], img[src$=".jpeg"]');
let completedCount = 0;
loader.style.display = 'flex';
progressBar.style.width = '0%';
progressBar.textContent = '0%';
images.forEach((img, index) => {
uploadImageToServer(img.src, index, images.length, function () {
completedCount++;
const progress = Math.floor((completedCount / images.length) * 100);
progressBar.style.width = progress + '%';
progressBar.textContent = progress + '%';
if (completedCount === images.length) {
loader.style.display = 'none';
}
});
});
});
function uploadImageToServer(imageUrl, index, total, callback) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, `downloaded_image_${index}.png`);
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayDownloadLink(data.url);
callback();
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
callback();
});
}
function displayDownloadLink(url) {
const linkElem = document.createElement('div');
linkElem.innerHTML = `<p>لینک دانلود: <a href="${url}" target="_blank">${url}</a></p>`;
const copyButton = document.createElement('button');
copyButton.textContent = 'کپی لینک 📋';
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(url).then(() => {
alert('لینک کپی شد');
});
});
linkElem.appendChild(copyButton);
linksContainer.appendChild(linkElem);
}
});
</script>
</body>
</html>
کد برای upload.php: این کد PHP تصویر را دریافت میکند و آن را ذخیره میکند.
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
// ساخت پوشه ذخیرهسازی در صورت عدم وجود
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
نکات مهم:
نوار پیشرفت : نشان دهنده پیشرفت عملیات آپلود است.
مدیریت خطا : درصورت بروز خطا در هر مرحله، عملیات مناسب انجام میشود.
دامنه : http://yourdomain.com در کد PHP باید با نام دامنه واقعی شما جایگزین شود.
پوشه storage : مطمئن شوید که پوشه ذخیرهسازی قابل نوشتن است و به درستی تنظیمشده باشد روی سرور شما.
این پیادهسازی باید کار شما را تکمیل کند، از جمله انجام عملیات بارگذاری تصویر در سرور و ارائه لینک دانلود به کاربر.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.link-container {
margin: 20px;
font-size: 14px;
color: green;
}
button {
margin-top: 10px;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصاویر 📥</button>
<div id="linksContainer" class="link-container"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const loader = document.getElementById('loader');
const progressBar = document.getElementById('bar');
const downloadButton = document.getElementById('downloadButton');
const linksContainer = document.getElementById('linksContainer');
function simulateProgress(callback) {
let progress = 0;
function updateProgress() {
progress += 1;
progressBar.style.width = progress + '%';
progressBar.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
}
}
updateProgress();
}
function showContent() {
loader.style.display = 'none';
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
};
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
let completedCount = 0;
loader.style.display = 'flex';
progressBar.style.width = '0%';
progressBar.textContent = '0%';
images.forEach(img => {
uploadImageToServer(img.src, function () {
completedCount++;
const progress = Math.floor((completedCount / images.length) * 100);
progressBar.style.width = progress + '%';
progressBar.textContent = progress + '%';
if (completedCount === images.length) {
loader.style.display = 'none';
}
});
});
});
function uploadImageToServer(imageUrl, callback) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayDownloadLink(data.url);
} else {
alert('خطا در آپلود تصویر');
}
callback();
})
.catch(error => {
console.error('خطا:', error);
callback();
});
}
function displayDownloadLink(url) {
const linkElem = document.createElement('div');
linkElem.innerHTML = `<p>لینک دانلود: <a href="${url}" target="_blank">${url}</a></p>`;
const copyButton = document.createElement('button');
copyButton.textContent = 'کپی لینک 📋';
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(url).then(() => {
alert('لینک کپی شد');
});
});
linkElem.appendChild(copyButton);
linksContainer.appendChild(linkElem);
}
});
</script>
</body>
</html>
کد برای upload.php: php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
// ساخت پوشه ذخیرهسازی در صورت عدم وجود
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
مخفی سازی و نوار پیشرفت : این کد یک نوار پیشرفت ساده دارد که بارگذاری تصاویر در حال انجام را نشان میدهد.
دامنه : در upload.php، حتماً http://yourdomain.com را به دامنه واقعی خود تغییر دهید.
تست و تعامل با Gradio : کل کد به گونهای تنظیم شده است که باید به درستی با سایت Gradio شما کار کند و نیازی به تغییر در این بخشها نیست، مگر دامنه مناسب و احراز هویت سرور برای اجازه دسترسی به این فایلها.
این پیادهسازی بر اساس اطلاعات و نیازهایی که ارائه کردهاید طراحی شده و باید شما را قادر به مدیریت و مشاهده عملکرد صحیح سایتتان کند. لطفاً توجه داشته باشید که دسترسی کامل به تصاویر شما باید ممکن باشد و همچنین اطمینان از تطابق با محدودیتهای CORS داشته باشید.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
});
const confirmButton = document.getElementById('confirmButton');
confirmButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const fullscreenImage = document.getElementById('fullscreenImage');
fullscreenImage.src = modalImage.src;
fullscreenOverlay.style.display = 'flex';
document.getElementById('saveNotification').style.display = 'none';
});
const closeFullscreenButton = document.getElementById('closeFullscreenButton');
closeFullscreenButton.addEventListener('click', function() {
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
fullscreenOverlay.style.display = 'none';
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div class="notification" id="uploadLinkNotification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<div class="notification error" id="noImageNotification">
<p>هیچ تصویر ساخته شده ای یافت نشد.</p>
<button id="noImageButton">باشه</button>
</div>
<div class="fullscreen-overlay" id="fullscreenOverlay">
<img id="fullscreenImage" class="fullscreen-image" />
<button class="close-fullscreen" id="closeFullscreenButton">❌</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
function modifyText() {
document.querySelectorAll('body *').forEach(element => {
element.childNodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
if (node.nodeValue.includes('GPU')) {
node.parentElement.style.display = 'none';
}
const replacements = {
'Examples': 'نمونه ها',
'Expected Ratio': 'اندازه مورد انتظار',
'Prompt': 'متن',
'Optional': 'انگلیسی وارد کنید و یا خالی بزارید.',
'Generate': 'شروع ساخت',
'Diffusers Image Outpaint': 'ابزار فوق پیشرفته گسترش تصاویر',
'Drop an image you would like to extend, pick your expected ratio and hit شروع ساخت': 'با نرم افزار هوش مصنوعی',
'Input Image': 'تصویر ورودی',
'Alignment': 'تراز',
'Advanced settings': 'تنظیمات پیشرفته',
'Middle': 'وسط',
'Left': 'چپ',
'Right': 'راست',
'Top': 'بالا',
'Bottom': 'پایین',
'Width': 'عرض',
'Steps': 'مراحل',
'Height': 'ارتفاع',
'Mask': 'ماسک',
'Overlap': 'همپوشانی',
'Resize input image': 'تغییر اندازه تصویر ورودی',
'Loading': 'در حال بارگذاری'
};
for (const [original, replacement] of Object.entries(replacements)) {
if (node.nodeValue.includes(original)) {
node.nodeValue = node.nodeValue.replace(original, replacement);
}
}
}
});
});
}
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
const copyLinkButton = document.getElementById('copyLinkButton');
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error)
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
</body>
</html>
upload.phpاین اسکریپت PHP همانطور که قبلاً توضیح دادهشده است، تصویر را بر روی سرور آپلود کرده و لینک دانلود را برمیگرداند.
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
JavaScript در زمان کلیک بر روی دکمه "ذخیره تصویر"، تصویر را به سرور آپلود میکند.
در صورت موفقیتآمیز بودن آپلود، لینک جدید در نوتیفیکیشن نمایش داده میشود و دکمهای برای کپی لینک به کلیپبورد وجود دارد.
لطفاً http://yourdomain.com را به آدرس واقعی دامنه خود تغییر دهید.
این پیادهسازی باید نیازهای شما را به خوبی پوشش دهد. اگر سوال یا چالشی دارید، بفرمایید.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.close-button {
background-color: #d9534f;
color: white;
}
</style>
</head>
<body>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<div class="notification error" id="noImageNotification">
<p>هیچ تصویر ساخته شده ای یافت نشد.</p>
<button id="noImageButton">باشه</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
if (images.length > 0) {
const modalImage = document.getElementById('modalImage');
modalImage.src = images[0].src; // نمایش اولین تصویر برای پیشنمایش
document.getElementById('imageModal').style.display = 'block';
} else {
document.getElementById('noImageNotification').style.display = 'block';
}
});
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'uploaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
copyLinkButton.addEventListener('click', function() {
navigator.clipboard.writeText(downloadLink.href).then(() => {
alert('لینک کپی شد');
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
document.getElementById('noImageNotification').style.display = 'none';
});
});
</script>
</body>
</html>
PHP Script (upload.php) این اسکریپت تصویر را در سروری که PHP روی آن در حال اجرا است آپلود میکند و لینک دانلود مستقیم ارائه میدهد.
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
نمایش نوتیفیکیشن: با اطمینان از اینکه URL شامل http://yourdomain.com به درستی در upload.php تغییر یافته تا دامنه واقعی شما منعکس شود کمک خواهد کرد.
خطایابی: اگر هنوز با مشکل مواجهاید، بررسی کنید که اسکریپتهای آپلود و سرور به درستی پیکربندی شدهاند و هیچ محدودیت آپلودی وجود ندارد.
CORS و تنظیمات امنیتی: بررسی کنید که تنظیمات CORS به درستی تنظیم شدهاند تا دانلود تصاویر از URLهای مختلف امکانپذیر باشد.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
const copyLinkButton = document.getElementById('copyLinkButton');
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error)
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
const copyLinkButton = document.getElementById('copyLinkButton');
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
}
نکات:
کنسول مرورگر : تمامی خطاها و پاسخی که از سرور میآید را در کنسول مرورگر بررسی کنید.
fetch و CORS : تأیید نمایید که تنظیمات CORS به درستی روی سرور انجام شده تا درخواست fetch موفق باشد.
تست با سرور محلی : در صورت امکان، ابتدا تستها را روی یک سرور محلی انجام دهید و سپس به محیط تولید منتقل کنید.
توجه به جزئیات در پاسخ سرور و کنسول مرورگر اغلب میتواند راهنمای خوبی برای شناسایی مشکلی باشد که در فرایندها وجود دارد.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.close-button {
background-color: #d9534f;
color: white;
}
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-left-color: #76c7c0;
animation: spin 1s linear infinite;
margin: 20px auto;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<div class="spinner"></div>
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
document.getElementById('saveImageButton').addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) توجه داشته باشید که yourdomain.com را با دامنه واقعی خود جایگزین کنید:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
HTML و JavaScript : تغییرات انجامشده اطمینان میدهند که تصویر به درستی آپلود شده و لینک دانلود در یک نوتیفیکیشن نشان داده شده و قابل کپی است.
PHP : اسکریپت به درستی فایل را آپلود کرده و لینک دانلود مستقیم برمیگرداند.
طراحی HTML و CSS : برای بهتر نشان دادن اسپینر و متن اعلان.
اجرای محلی و تست : ابتدا روی لوکال یا محیط تستی بررسی کرده و سپس به سرور اصلی انتقال دهید.
تنظیمات سرور و PHP : اطمینان حاصل کنید که فایلها مجاز به آپلود در مسیر مشخص شده هستند و no error is present with CORS یا limitations.
با انجام این مراحل، باید بتوانید تصاویر را به سادگی آپلود و مدیریت کنید.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.close-button {
background-color: #d9534f;
color: white;
}
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-left-color: #76c7c0;
animation: spin 1s linear infinite;
margin: 20px auto;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<div class="spinner"></div>
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) و این هم کد PHP برای آپلود تصویر:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'https://www.aisada.ir/hamed/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
هدف دامنه : در کد PHP، آدرس کره شده به https://www.aisada.ir/hamed/storage/ جایگزین شده است، اطمینان حاصل کنید که پوشه storage به درستی تنظیم شده و دسترسی لازم برای نوشتن به آن داده شده است.
فایلها : دو فایل اصلی index.php و upload.php باید در سرور آپلود شوند. فایل upload.php باید در کنار index.php قرار بگیرد.
تنظیمات سرور : اطمینان حاصل کنید که هیچ محدودیت امنیتی یا تنظیمات CORS در سرور شما مانع عملکرد صحیح نمیشود.
این مجموعه باید به شما کمک کند تا تصاویر را آپلود و لینکهای دانلود آنها را با استفاده دامنه دلخواه خودتان مشاهده کنید. اگر با مشکلی مواجه شدید یا سوالی دارید، لطفاً بپرسید.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
});
const confirmButton = document.getElementById('confirmButton');
confirmButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const fullscreenImage = document.getElementById('fullscreenImage');
fullscreenImage.src = modalImage.src;
fullscreenOverlay.style.display = 'flex';
document.getElementById('saveNotification').style.display = 'none';
});
const closeFullscreenButton = document.getElementById('closeFullscreenButton');
closeFullscreenButton.addEventListener('click', function() {
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
fullscreenOverlay.style.display = 'none';
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 20px;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<div class="spinner"></div>
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) توجه کنید که yourdomain.com با دامنهی شما جایگزین شود:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'https://www.aisada.ir/hamed/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات نهایی:
کد HTML و JavaScript تمامی رفتارهای تقاضا شده شامل نوار بارگیری و اطلاعات متنی شما را حفظ میکند.
Set the path https://www.aisada.ir/hamed/storage/ در کد PHP مطابق با مسیر حقیقی فایل در هاست شما تنظیم شده است.
توجه کنید که فایلهای HTML و PHP را در مسیر مناسب قرار دهید و اطمینان از دسترسی و مجوز نوشتن در پوشهی storage حاصل کنید تا عملیات به درستی انجام شود.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 20px;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<div class="spinner"></div>
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) این کد PHP مسئول ذخیرهسازی تصویر و ارسال لینک دانلود آن است. لطفاً http://yourdomain.com را با دامنهی واقعی خود جایگزین کنید:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'https://www.aisada.ir/hamed/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات نهایی:
مسیرهای فایل : اطمینان حاصل کنید که مسیر storage/ بر روی سرور شما درست تنظیم شده و سطح دسترسی مناسب (مثل 0777 برای تست) به آن داده شده است.
CORS و تنظیمات امنیتی : اطمینان حاصل کنید گزینههای امنیت سرور مانند CORS اجازه دسترسی به http درخواستها را میدهند.
تست اجرا : فایلها را بر روی سرور آپلود کرده و صحت عملکرد آنها را از طریق مرورگر خود امتحان کنید.
این مجموعه باید بهصورت کامل کار کند و تمامی مراحل مورد نیاز شما را پوشش دهد. اگر به مشکلی برخوردید یا سوالی داشتید، خوشحال میشوم که کمک کنم.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
const copyLinkButton = document.getElementById('copyLinkButton');
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error)
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
/* استایلها برای مخفیسازی و نمای گرافیکی */
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
</style>
</head>
<body>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
// وقتی کلیک روی ذخیره انجام میشود
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('https://www.aisada.ir/hamed/upload.php', {
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) این فایل باید روی سرور شما در مسیر مورد نظر قرار گیرد:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'https://www.aisada.ir/hamed/storage/' . $fileName;
}
}
echo json_encode($response);
نکات نهایی:
اطمینان از صحت مسیر : upload.php باید در مسیر مشخص شده روی سرور شما وجود داشته باشد.
اجازه دسترسی و سطحدسترسی فولدر : فولدر storage باید روی سرور قابل نوشتن باشد (به عنوان مثال سطح دسترسی 0777 برای تست).
تنظیمات امنیتی : اطمینان حاصل کنید که از لحاظ امنیتی، تنظیمات CORS و دیگر مسائل به خوبی تنظیم شده تا دسترسی و ارسال دیتا از مبداهای مختلف به درستی انجام شود.
این تنظیمات باید شما را قادر سازد کد را در هر صفحهای که نیاز دارید استفاده کنید و تصاویر را مستقیماً به سرورتان آپلود و لینکهای مستقیمشان را دریافت کنید.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
});
const confirmButton = document.getElementById('confirmButton');
confirmButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const fullscreenImage = document.getElementById('fullscreenImage');
fullscreenImage.src = modalImage.src;
fullscreenOverlay.style.display = 'flex';
document.getElementById('saveNotification').style.display = 'none';
});
const closeFullscreenButton = document.getElementById('closeFullscreenButton');
closeFullscreenButton.addEventListener('click', function() {
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
fullscreenOverlay.style.display = 'none';
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('https://www.aisada.ir/hamed/upload.php', { // به مسیر آپلود در سرور شما اشاره میکند
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) این اسکریپت باید در سرور شما در مسیر https://www.aisada.ir/hamed/upload.php وجود داشته باشد:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/'; // مسیر ذخیرهسازی
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'https://www.aisada.ir/hamed/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات نهایی:
اتصال به سرور : اطمینان حاصل کنید که مسیرهای سرور درست هستند و فایل upload.php در مسیر صحیح قرار دارد.
دسترسی فولدر storage : فولدر storage باید سطح دسترسی نوشتن داشته باشد تا فایلها به درستی ذخیره شوند.
سازگاری با مرورگرها و تنظیمات امنیتی : از تنظیمات CORS و مسائل امنیتی مشابه اطمینان حاصل کنید تا درخواستهای AJAX به درستی کار کنند.
با این تنظیمات کامل، باید بتوانید از این کد در هر جایی استفاده کنید و تصاویر را به سرور آپلود کرده و لینکهای مربوطه با دامنه خودتان بگیرید.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="https://gradio.s3-us-west-2.amazonaws.com/4.44.0/gradio.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
saveNotification.style.display = 'block';
fetch(modalImage.src)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('https://www.aisada.ir/hamed/upload.php', { // آدرس آپلود در سرور شما
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
});
</script>
</body>
</html>
کد PHP (upload.php) این کد را به عنوان فایل upload.php بر روی سرور خود ذخیره کنید:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/'; // مسیر فولدر ذخیرهسازی
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'https://www.aisada.ir/hamed/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات نهایی:
مسیردهی صحیح : اطمینان حاصل کنید که فایل upload.php بهدرستی بر روی سرور و مسیر مشخص شده قرار دارد.
دسترسی مناسب پوشه storage : مطمئن شوید که فولدر storage در هاست شما سطح دسترسی کافی برای نوشتن دارد.
عملکرد مشترک : این کد باید قادر باشد تصاویر را به درستی آپلود کند و لینک آنها را برای کاربر فراهم کند.
با این پیادهسازی، شما قادر به آپلود و ذخیره تصاویر در سرور خود خواهید بود و لینک دانلود آنها را به کاربران میتوانید نشان دهید.
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#tutorialTitle {
color: red;
}
#einsteinTip {
margin-top: 10px;
font-style: italic;
color: #555;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
code Copy code
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
});
const confirmButton = document.getElementById('confirmButton');
confirmButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
const fullscreenImage = document.getElementById('fullscreenImage');
fullscreenImage.src = modalImage.src;
fullscreenOverlay.style.display = 'flex';
document.getElementById('saveNotification').style.display = 'none';
});
const closeFullscreenButton = document.getElementById('closeFullscreenButton');
closeFullscreenButton.addEventListener('click', function() {
const fullscreenOverlay = document.getElementById('fullscreenOverlay');
fullscreenOverlay.style.display = 'none';
});
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<div class="notification error" id="noImageNotification">
<p>هیچ تصویر ساخته شده ای یافت نشد.</p>
<button id="noImageButton">باشه</button>
</div>
<div class="fullscreen-overlay" id="fullscreenOverlay">
<img id="fullscreenImage" class="fullscreen-image" />
<button class="close-fullscreen" id="closeFullscreenButton">❌</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
function modifyText() {
document.querySelectorAll('body *').forEach(element => {
element.childNodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
if (node.nodeValue.includes('GPU')) {
node.parentElement.style.display = 'none';
}
const replacements = {
'Examples': 'نمونه ها',
'Expected Ratio': 'اندازه مورد انتظار',
'Prompt': 'متن',
'Optional': 'انگلیسی وارد کنید و یا خالی بزارید.',
'Generate': 'شروع ساخت',
'Diffusers Image Outpaint': 'ابزار فوق پیشرفته گسترش تصاویر',
'Drop an image you would like to extend, pick your expected ratio and hit شروع ساخت': 'با نرم افزار هوش مصنوعی',
'Input Image': 'تصویر ورودی',
'Alignment': 'تراز',
'Advanced settings': 'تنظیمات پیشرفته',
'Middle': 'وسط',
'Left': 'چپ',
'Right': 'راست',
'Top': 'بالا',
'Bottom': 'پایین',
'Width': 'عرض',
'Steps': 'مراحل',
'Height': 'ارتفاع',
'Mask': 'ماسک',
'Overlap': 'همپوشانی',
'Resize input image': 'تغییر اندازه تصویر ورودی',
'Loading': 'در حال بارگذاری'
};
for (const [original, replacement] of Object.entries(replacements)) {
if (node.nodeValue.includes(original)) {
node.nodeValue = node.nodeValue.replace(original, replacement);
}
}
}
});
});
}
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', { // به مسیر آپلود بر روی سرور اشاره میکند
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
</body>
</html>
کد PHP (upload.php) این کد باید روی سرور شما قرار گیرد و مسئول ذخیرهسازی تصویر و ایجاد لینک دانلود باشد:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
توضیحات:
جایگزینی http://yourdomain.com : مطمئن شوید که http://yourdomain.com را با دامنه دقیق خود در کد PHP جایگزین کنید.
دسترسی به پوشه storage : اطمینان حاصل کنید که پوشه storage حقوق کافی برای نوشتن دارد.
کد جاوااسکریپت و سازگاری : کد جاوااسکریپت برای ارسال درخواست AJAX به سرورتان طراحی شده و به صفحه مورد نظر شما متصل میشود.
<?php
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
echo json_encode($response);
exit;
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<div class="notification error" id="noImageNotification">
<p>هیچ تصویر ساخته شده ای یافت نشد.</p>
<button id="noImageButton">باشه</button>
</div>
<div class="fullscreen-overlay" id="fullscreenOverlay">
<img id="fullscreenImage" class="fullscreen-image" />
<button class="close-fullscreen" id="closeFullscreenButton">❌</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
function modifyText() {
document.querySelectorAll('body *').forEach(element => {
element.childNodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
if (node.nodeValue.includes('GPU')) {
node.parentElement.style.display = 'none';
}
const replacements = {
'Examples': 'نمونه ها',
'Expected Ratio': 'اندازه مورد انتظار',
'Prompt': 'متن',
'Optional': 'انگلیسی وارد کنید و یا خالی بزارید.',
'Generate': 'شروع ساخت',
'Diffusers Image Outpaint': 'ابزار فوق پیشرفته گسترش تصاویر',
'Drop an image you would like to extend, pick your expected ratio and hit شروع ساخت': 'با نرم افزار هوش مصنوعی',
'Input Image': 'تصویر ورودی',
'Alignment': 'تراز',
'Advanced settings': 'تنظیمات پیشرفته',
'Middle': 'وسط',
'Left': 'چپ',
'Right': 'راست',
'Top': 'بالا',
'Bottom': 'پایین',
'Width': 'عرض',
'Steps': 'مراحل',
'Height': 'ارتفاع',
'Mask': 'ماسک',
'Overlap': 'همپوشانی',
'Resize input image': 'تغییر اندازه تصویر ورودی',
'Loading': 'در حال بارگذاری'
};
for (const [original, replacement] of Object.entries(replacements)) {
if (node.nodeValue.includes(original)) {
node.nodeValue = node.nodeValue.replace(original, replacement);
}
}
}
});
});
}
function simulateProgress(callback) {
let progress = 0;
const progressElement = document.getElementById('bar');
function updateProgress() {
progress += 1;
if (progress > 100) progress = 100;
progressElement.style.width = progress + '%';
progressElement.textContent = progress + '%';
if (progress < 100) {
setTimeout(updateProgress, 50);
} else {
callback();
modifyText();
}
}
updateProgress();
}
function showContent() {
document.getElementById('loader').style.display = 'none';
const gradioApp = document.querySelector('gradio-app');
gradioApp.scrollIntoView({ behavior: 'smooth', block: 'start' });
document.getElementById('downloadButton').style.display = 'block';
}
window.onload = function() {
simulateProgress(showContent);
const gradioApp = document.querySelector('gradio-app');
function checkIfLoaded() {
if (gradioApp) {
gradioApp.addEventListener('gradio_loaded', showContent);
gradioApp.addEventListener('load', showContent);
setTimeout(showContent, 10000);
} else {
setTimeout(checkIfLoaded, 100);
}
}
checkIfLoaded();
};
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
modifyText();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
const downloadButton = document.getElementById('downloadButton');
if (downloadButton) {
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
}
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
const saveImageButton = document.getElementById('saveImageButton');
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('', { // از آدرس فعلی برای ارسال به PHP استفاده میکند
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
const copyLinkButton = document.getElementById('copyLinkButton');
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
</body>
</html>
توضیحات:
کد PHP : در خود فایل index.php قرار داده شده و مسئول آپلود و مدیریت تصاویر است.
کد HTML و JavaScript : دارای تمام عناصر و عملکردی است که شما درخواست کردهاید.
این پیادهسازی به شما امکان میدهد کد را در یک فایل استفاده کرده و نیازی به جدا کردن فایل PHP نداشته باشید. در صورت بروز هر گونه مشکلی، لطفاً جزئیات بیشتر را ارائه دهید تا بتوانم بهتر کمک کنم.
نکات کلیدی:
مسیر storage بر روی سرور باید موجود و دارای مجوزهای مناسب باشد.
yourdomain.com را با دامنه واقعی خود جایگزین کنید.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<div class="notification error" id="noImageNotification">
<p>هیچ تصویر ساخته شده ای یافت نشد.</p>
<button id="noImageButton">باشه</button>
</div>
<div class="fullscreen-overlay" id="fullscreenOverlay">
<img id="fullscreenImage" class="fullscreen-image" />
<button class="close-fullscreen" id="closeFullscreenButton">❌</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('upload.php', { // به مسیر فایل PHP بروی سرور اشاره دارد
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
</body>
</html>
۲. فایل PHP (upload.php) این فایل را در سرور خود بارگذاری کنید تا تصاویر را دریافت و ذخیرهکند:
php Copy code
<?php
$storagePath = __DIR__ . '/storage/'; // مسیر فولدر ذخیرهسازی
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
}
echo json_encode($response);
نکات:
اطمینان حاصل کنید که http://yourdomain.com را با دامنه واقعی خود جایگزین کنید.
سطح دسترسی پوشه storage باید به گونهای تنظیم شود که قابلنوشتن باشد (به عنوان مثال 0777 برای آزمایش).
کد جاوااسکریپت تصاویر را به upload.php ارسال میکند که فایلها روی سرور ذخیره شوند و لینک آنها بازگردانده شود.
این تنظیمات باید به شما کمک کند تا یک فرآیند کامل را از طریق HTML و PHP به اجرا در آورید. اگر سوال یا مشکل دیگری دارید، خوشحال میشوم که کمک کنم.
<?php
// کد PHP برای ذخیرهسازی فایلهای آپلود شده
$storagePath = __DIR__ . '/storage/';
if (!is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$response = ['success' => false, 'url' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp'];
if (!in_array($fileExtension, $allowedExtensions)) {
echo json_encode($response);
exit;
}
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $storagePath . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$response['success'] = true;
$response['url'] = 'http://yourdomain.com/storage/' . $fileName;
}
echo json_encode($response);
exit;
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
background-color: #f4f4f9;
font-family: sans-serif;
position: relative;
}
a {
display: none;
}
#loader {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f4f9;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
text-align: center;
}
#progress {
width: 80%;
height: 30px;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#bar {
width: 0;
height: 100%;
background-color: #76c7c0;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.2s ease;
}
gradio-app {
display: flex;
justify-content: center;
align-items: center;
zoom: 0.8;
min-height: 100vh;
margin-top: 0;
}
.modal, .notification {
display: none;
position: fixed;
z-index: 10002;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
text-align: center;
color: #333;
max-width: 400px;
width: 90%;
}
.modal img, .notification img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
border-radius: 8px;
}
.modal button, .notification button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
margin: 5px;
font-size: 16px;
}
.modal button:hover, .notification button:hover {
background-color: #45a049;
}
.close-button {
background-color: #d9534f;
color: white;
}
.close-button:hover {
background-color: #c9302c;
}
#downloadButton {
display: none;
position: fixed;
bottom: 20px;
left: 20px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 5px;
z-index: 10003;
font-size: 16px;
}
#downloadButton:hover {
background-color: #45a049;
}
.fullscreen-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10004;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fullscreen-image {
max-width: 90%;
max-height: 90%;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.close-fullscreen {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: white;
font-size: 24px;
border: none;
cursor: pointer;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div id="loader">
<div id="progress">
<div id="bar">0%</div>
</div>
<div>لطفاً صبر کنید...</div>
<div id="einsteinTip">اگر این هوش مصنوعی باز نشد فیلتر شکن تونو روشن کنید</div>
</div>
<gradio-app src="https://victorestrada-diffusers-image-outpaint.hf.space/"></gradio-app>
<button id="downloadButton">دانلود تصویر 📥</button>
<div class="modal" id="imageModal">
<h3>پیشنمایش تصویر</h3>
<img id="modalImage" alt="Image Preview" />
<button id="nextImageButton">تصویر بعدی ⏭️</button>
<button id="saveImageButton">ذخیره تصویر 💾</button>
<button class="close-button">لغو ❌</button>
</div>
<div class="notification" id="saveNotification">
<p>در حال آپلود تصویر...</p>
</div>
<div id="uploadLinkNotification" class="notification">
<p>لینک دانلود تصویر: <a href="#" id="downloadLink" target="_blank"></a></p>
<button id="copyLinkButton">کپی لینک 📋</button>
<button class="close-button">بستن</button>
</div>
<div class="notification error" id="noImageNotification">
<p>هیچ تصویر ساخته شده ای یافت نشد.</p>
<button id="noImageButton">باشه</button>
</div>
<div class="fullscreen-overlay" id="fullscreenOverlay">
<img id="fullscreenImage" class="fullscreen-image" />
<button class="close-fullscreen" id="closeFullscreenButton">❌</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
const saveImageButton = document.getElementById('saveImageButton');
const saveNotification = document.getElementById('saveNotification');
const uploadLinkNotification = document.getElementById('uploadLinkNotification');
const downloadLink = document.getElementById('downloadLink');
const copyLinkButton = document.getElementById('copyLinkButton');
downloadButton.addEventListener('click', function() {
const images = document.querySelectorAll('gradio-app img[src$=".webp"], gradio-app img[src$=".png"], gradio-app img[src$=".jpg"], gradio-app img[src$=".jpeg"]');
const ignoredUrl = 'https://victorestrada-diffusers-image-outpaint.hf.space/file=/tmp/gradio/58d27ee015923ff8867291e88910c3b1fc5e49698a7a06372037c6d58cd5452e/example_1.webp';
let imageArray = [];
images.forEach(img => {
if (!img.src.includes(ignoredUrl)) {
imageArray.push(img.src);
}
});
if (imageArray.length > 0) {
let currentIndex = 0;
displayImage(imageArray, currentIndex);
document.getElementById('nextImageButton').onclick = function() {
currentIndex = (currentIndex + 1) % imageArray.length;
displayImage(imageArray, currentIndex);
};
} else {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'block';
}
});
function displayImage(imageList, index) {
const modalImage = document.getElementById('modalImage');
modalImage.src = imageList[index];
const imageModal = document.getElementById('imageModal');
imageModal.style.display = 'block';
}
saveImageButton.addEventListener('click', function() {
const modalImage = document.getElementById('modalImage');
uploadImageToServer(modalImage.src);
});
function uploadImageToServer(imageUrl) {
const saveNotification = document.getElementById('saveNotification');
saveNotification.style.display = 'block';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.png');
return fetch('', { // درخواست به خود فایل برای پردازش PHP
method: 'POST',
body: formData,
});
})
.then(response => response.json())
.then(data => {
saveNotification.style.display = 'none';
if (data.success) {
downloadLink.href = data.url;
downloadLink.textContent = data.url;
uploadLinkNotification.style.display = 'block';
copyLinkButton.onclick = function() {
navigator.clipboard.writeText(data.url).then(() => {
alert('لینک کپی شد');
});
};
} else {
alert('خطا در آپلود تصویر');
}
})
.catch(error => {
console.error('خطا:', error);
saveNotification.style.display = 'none';
});
}
document.querySelectorAll('.close-button').forEach(button => {
button.addEventListener('click', function(event) {
event.stopPropagation();
button.closest('.modal, .notification').style.display = 'none';
});
});
const noImageButton = document.getElementById('noImageButton');
noImageButton.addEventListener('click', function() {
const noImageNotification = document.getElementById('noImageNotification');
noImageNotification.style.display = 'none';
});
document.addEventListener('click', function(event) {
if (!event.target.closest('.modal, .notification') && !event.target.matches('#downloadButton')) {
document.querySelectorAll('.modal, .notification').forEach(modal => {
modal.style.display = 'none';
});
}
});
});
</script>
</body>
</html>
توضیحات:
ترکیب کدها : این فایل دارای هر دو بخش HTML و PHP است و برای اجرای صحیح به عنوان فایل .php باید ذخیره شود.
نحوه عملکرد : جاوااسکریپت با فراخوانی PHP از طریق fetch آپلود را انجام میدهد و PHP فایل را پردازش و URL آن را برمیگرداند.
مسیرهای مناسب : http://yourdomain.com باید با دامنه واقعی شما جایگزین شود و اطمینان حاصل شود که فولدر storage دارای سطح دسترسی مناسب است.
اگر سوال یا مشکلی دیگری وجود دارد، خوشحال میشوم که کمک کنم.