محمد
3 سال پیش توسط محمد مطرح شد
1 پاسخ

کم حجم کردن حجم تصاویر و فیلم

سلام
دویتان چجور با php یا جاوا اسکریپت حجم فایل هارو کم کنم


ثبت پرسش جدید
مهدی شاه عباسیان
تخصص : برنامه نویس
@shahabbasian 3 سال پیش آپدیت شد
0

در php میتونید از کتابخونه imagine استفاده کنید:

https://imagine.readthedocs.io/en/stable/

یا اگر بخواهید از توابع خود php اسفاده کنید :

$src = "address/filename.jpg"

$src = ImageCreateFromJPEG($src);
$sw = ImageSX($src);
$sh = ImageSY($src);
$dst = ImageCreateTrueColor($sw, $sh);
ImageCopy($dst, $src, 0, 0, 0, 0, $sw, $sh);
ImageJPEG($dst, $path . $fileName . '.jpg', 100);

اون عدد 100 در آخر کیفیت تصویر رو تایین میکنه
و چرا کد های بالاش رو نوشتم ؟
چون بتونم در ادامه با کد زیر یک فایل کوچکتر هم از همون عکس بسازم تا در صفحه اصلی از فایل کوچکتر استفاده کنم و در جای لازم فایل اصلی با اندازه اصلی اینطوری شاید لازم هم نشد کیفیت عکس رو زیاد بیارم پایین که برای سئو مناسب نیست:

$dw = 300;
$dh = $dw / ($sw / $sh);
$dst = ImageCreateTrueColor($dw, $dh);
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $dw, $dh, $sw, $sh);
ImageJPEG($dst, $path . $fileName . '_t.jpg', 100);
imagedestroy($src);
imagedestroy($dst);

در کدهای بالا $path رو آدرس پوشه ای که قراره فایل اونجا ذخیره بشه رو و در $fileName نام فایل که قراره ذخیره بشه رو وارد کنید

و برای فایل png:

$src = ImageCreateFromPNG($src);
$sw = ImageSX($src);
$sh = ImageSY($src);
$dst = ImageCreateTrueColor($sw, $sh);
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($dst , 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($dst, $background);

// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($dst, false);

// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);
ImageCopy($dst, $src, 0, 0, 0, 0, $sw, $sh);
ImagePNG($dst, $path . $fileName . '.png', 7);
$dw = 300;
$dh = $dw / ($sw / $sh);
$dst = ImageCreateTrueColor($dw, $dh);
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($dst , 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($dst, $background);

// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($dst, false);

// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $dw, $dh, $sw, $sh);
ImagePNG($dst, $path . $fileName . '_t.png', 7);
imagedestroy($src);
imagedestroy($dst);

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام