Updated upload Class (Png & thumbnail folder support)

1. Transparency support 2. Thumbnail generation

31

Arun Kr.
24-Jul-25

Upload.php ( Class File )

 

<?php

class upload
{
	public $ext;
	public $upload_ext_allow = array('png','jpg','jpeg','webp','gif','zip','pdf','mp4','mov','wmn','avi','mp3','doc','docx','xls','xlsx','ppt','pptx');

	public function All_file_upload($myarray)
	{
		extract($myarray);
		$this->check_dir($dir);

		if (!empty(array_intersect($upload_type, $this->upload_ext_allow)) == 1) {
			$this->ext = explode('.', $myfile['name']);
			$this->ext = strtolower(end($this->ext));

			if (in_array($this->ext, $upload_type)) {
				return $this->move_file(
					$myfile, $dir, $rename_name, $convert_status,
					$height ?? null, $width ?? null,
					$thumbnail ?? false, $thumb_width ?? 150, $thumb_height ?? 150,
					$thumb_dir ?? $dir // default to main dir if not specified
				);
			} else {
				return array('status' => '0', 'sms' => 'Please Submit Only '.implode(', ', $upload_type));
			}
		}
	}

	public function move_file($myfile, $dir, $rename_name, $convert_status, $height = null, $width = null, $thumbnail = false, $thumb_width = 150, $thumb_height = 150, $thumb_dir = null)
	{
		$newname = date('Ymdhis') . "_" . rand(10, 100) . "_" . $rename_name . '.' . $this->ext;
		$uploadname = str_replace(' ', '', $dir . $newname);

		if (move_uploaded_file($myfile['tmp_name'], $uploadname)) {
			if (strtolower($convert_status) == 'yes' && in_array($this->ext, ['jpg', 'jpeg', 'png', 'gif'])) {
				$response = $this->webp_convert($uploadname, $dir, $rename_name, $height, $width, $thumbnail, $thumb_width, $thumb_height, $thumb_dir);
				unlink($uploadname); // delete original after conversion
				return $response;
			} else {
				if ($thumbnail && in_array($this->ext, ['jpg', 'jpeg', 'png', 'gif'])) {
					$this->create_thumbnail($uploadname, $thumb_dir, $rename_name, $thumb_width, $thumb_height);
				}
				return array('status' => '1', 'sms' => $newname);
			}
		} else {
			return array('status' => '0', 'sms' => 'File Upload Error in '.$newname);
		}
	}

	public function check_dir($dir)
	{
		if (!is_dir($dir)) {
			mkdir($dir, 0777, true);
		}
	}

	public function webp_convert($filepath, $dir, $rename_name, $height = null, $width = null, $thumbnail = false, $thumb_width = 150, $thumb_height = 150, $thumb_dir = null)
	{
		$image = $this->create_image_resource($filepath);
		if (!$image) {
			return array('status' => '0', 'sms' => 'Image conversion failed. Unsupported image.');
		}

		$orig_width = imagesx($image);
		$orig_height = imagesy($image);

		$target_width = $orig_width;
		$target_height = $orig_height;

		if ($width && $height) {
			$src_ratio = $orig_width / $orig_height;
			$target_ratio = $width / $height;

			if ($src_ratio > $target_ratio) {
				$target_width = $width;
				$target_height = intval($width / $src_ratio);
			} else {
				$target_height = $height;
				$target_width = intval($height * $src_ratio);
			}
		}

		$resized = imagecreatetruecolor($width ?? $target_width, $height ?? $target_height);
		imagesavealpha($resized, true);
		$transparent = imagecolorallocatealpha($resized, 0, 0, 0, 127);
		imagefill($resized, 0, 0, $transparent);

		$dst_x = ($width && $width > $target_width) ? intval(($width - $target_width) / 2) : 0;
		$dst_y = ($height && $height > $target_height) ? intval(($height - $target_height) / 2) : 0;

		imagecopyresampled($resized, $image, $dst_x, $dst_y, 0, 0, $target_width, $target_height, $orig_width, $orig_height);

		$newname = date('Ymdhis') . "_" . rand(10, 100) . "_" . $rename_name . '.webp';
		$output = $dir . $newname;
		imagewebp($resized, $output);

		if ($thumbnail) {
			$this->create_thumbnail($output, $thumb_dir, $rename_name, $thumb_width, $thumb_height, 'webp');
		}

		imagedestroy($image);
		imagedestroy($resized);

		return array('status' => '1', 'sms' => $newname);
	}

	private function create_image_resource($filepath)
	{
		$ext = strtolower(pathinfo($filepath, PATHINFO_EXTENSION));

		switch ($ext) {
			case 'jpg':
			case 'jpeg':
				return imagecreatefromjpeg($filepath);
			case 'png':
				return imagecreatefrompng($filepath);
			case 'gif':
				return imagecreatefromgif($filepath);
			case 'webp':
				return imagecreatefromwebp($filepath);
			default:
				return false;
		}
	}

	private function create_thumbnail($filepath, $thumb_dir, $rename_name, $thumb_width, $thumb_height, $format = null)
	{
		$this->check_dir($thumb_dir); // ensure thumbnail folder exists

		$image = $this->create_image_resource($filepath);
		if (!$image) return false;

		$orig_width = imagesx($image);
		$orig_height = imagesy($image);

		$src_ratio = $orig_width / $orig_height;
		$target_ratio = $thumb_width / $thumb_height;

		if ($src_ratio > $target_ratio) {
			$new_width = $thumb_width;
			$new_height = intval($thumb_width / $src_ratio);
		} else {
			$new_height = $thumb_height;
			$new_width = intval($thumb_height * $src_ratio);
		}

		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		imagesavealpha($thumb, true);
		$transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
		imagefill($thumb, 0, 0, $transparent);

		$dst_x = ($thumb_width - $new_width) / 2;
		$dst_y = ($thumb_height - $new_height) / 2;

		imagecopyresampled($thumb, $image, $dst_x, $dst_y, 0, 0, $new_width, $new_height, $orig_width, $orig_height);

		$thumb_ext = $format ?? pathinfo($filepath, PATHINFO_EXTENSION);
		$thumb_name = date('Ymdhis') . "_" . rand(10, 100) . "_" . $rename_name . '_thumb.' . $thumb_ext;
		$output = $thumb_dir . $thumb_name;

		if ($thumb_ext == 'webp') {
			imagewebp($thumb, $output);
		} else {
			imagepng($thumb, $output);
		}

		imagedestroy($image);
		imagedestroy($thumb);
	}
}

Usage with Thumbnail Folder

 

$upload = new upload();

$response = $upload->All_file_upload([
	'myfile' => $_FILES['image'],
	'dir' => 'uploads/',
	'rename_name' => 'sample_image',
	'convert_status' => 'yes',
	'upload_type' => ['jpg', 'jpeg', 'png', 'gif'],
	'height' => 800,
	'width' => 1200,
	'thumbnail' => true,
	'thumb_width' => 300,
	'thumb_height' => 200,
	'thumb_dir' => 'uploads/thumbs/' // ← this is the thumbnail folder
]);

echo json_encode($response);

 

Note -

  1. imagewebp($resized, $output, 90); // 90 = high quality
  2. imagepng($thumb, $output, 0); // 0 = best quality

 

@Since 2024 Arun'Log Powered by Arun Git