بله این کار خیلی راحته. برای مثال:
function upload3($sourcePath) {
$result = ['code' => 0];
if (!file_exists($sourcePath)) {
return json_encode($result);
}
$src = ImageCreateFromJPEG($sourcePath);
$w = ImageSX($src);
$h = ImageSY($src);
$dst = ImageCreateTrueColor($w, $h);
$tm1 = ImageCreateTrueColor(320, 240);
$tm2 = ImageCreateTrueColor(160, 120);
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $w, $h, $w, $h);
ImageCopyResampled($tm1, $src, 0, 0, 0, 0, 320, 240, $w, $h);
ImageCopyResampled($tm2, $src, 0, 0, 0, 0, 160, 120, $w, $h);
$fileName = time() . '.jpg';
$dstFN = 'uploads/original/' . $fileName;
$tm1FN = 'uploads/thumbone/' . $fileName;
$tm2FN = 'uploads/thumbtwo/' . $fileName;
ImageJPEG($dst, $dstFN, 100);
ImageJPEG($tm1, $tm1FN, 100);
ImageJPEG($tm2, $tm2FN, 100);
ImageDestroy($dst);
ImageDestroy($tm1);
ImageDestroy($tm2);
ImageDestroy($src);
$result = [
'code' => 1,
'original' => $dstFN,
'thumbone' => $tm1FN,
'thumbtwo' => $tm2FN,
];
return json_encode($result);
}
مثالی از نحوهی استفاده:
if (isset($_FILES['photo'])) {
$json = upload3($_FILES['photo']['tmp_name']);
}