Skip to content

Instantly share code, notes, and snippets.

@pranid
Forked from zeuxisoo/ReadMe.txt
Created December 11, 2017 12:11
Show Gist options
  • Save pranid/b68163b6da0e15d4df603790a91c0049 to your computer and use it in GitHub Desktop.
Save pranid/b68163b6da0e15d4df603790a91c0049 to your computer and use it in GitHub Desktop.
Simple upload and resize image on PHP
- attachment
- 1.jpg
- 2.jpg
...
...
...
- index.php
- resize_image.php
- uploader.php
<?php
if (isset($_POST['action']) && $_POST['action'] == "upload") {
include_once "uploader.php";
include_once "resize_image.php";
$uploader = new Uploader($_FILES['files']);
$uploader->set_upload_to("./attachment");
$uploader->set_valid_extensions(array('jpg', 'png', 'bmp'));
$uploader->set_resize_image_library(new ResizeImage());
if ($uploader->is_valid_extension() === false) {
echo "<p>Error</p>";
print_r($uploader->get_errors());
}else{
if ($uploader->run() === false) {
echo "<p>Error</p>";
print_r($uploader->get_errors());
}else{
echo "...Uploaded";
if ($uploader->resize(70) === false) {
echo "<p>Error</p>";
print_r($uploader->get_errors());
}else{
echo "...Resized";
}
}
}
exit;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload" />
1: <input type="file" name="files[]" /><br />
2: <input type="file" name="files[]" /><br />
3: <input type="file" name="files[]" />
<input type="submit" value="Upload" />
</form>
<?php
/*
$image = new ResizeImage();
$image->init('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');
$image->init('picture.jpg');
$image->resize_to_width(250);
$image->save('picture2.jpg');
$image->init('picture.jpg');
$image->scale(50);
$image->save('picture2.jpg');
$image->init('picture.jpg');
$image->resize_to_height(500);
$image->save('picture2.jpg');
$image->resize_to_height(200);
$image->save('picture3.jpg');
$image->init('picture.jpg');
$image->resize_to_width(150);
$image->output();
*/
class ResizeImage {
private $image;
private $image_type;
public function init($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
switch($this->image_type) {
case IMAGETYPE_JPEG:
$this->image = imagecreatefromjpeg($filename);
break;
case IMAGETYPE_GIF:
$this->image = imagecreatefromgif($filename);
break;
case IMAGETYPE_PNG:
$this->image = imagecreatefrompng($filename);
break;
}
}
public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
switch($image_type) {
case IMAGETYPE_JPEG:
imagejpeg($this->image, $filename, $compression);
break;
case IMAGETYPE_GIF:
imagegif($this->image,$filename);
break;
case IMAGETYPE_PNG:
imagepng($this->image,$filename);
break;
default:
return false;
break;
}
if($permissions != null) {
chmod($filename, $permissions);
}
return true;
}
public function output($image_type = IMAGETYPE_JPEG) {
switch($image_type) {
case IMAGETYPE_JPEG:
imagejpeg($this->image);
break;
case IMAGETYPE_GIF:
imagegif($this->image);
break;
case IMAGETYPE_PNG:
imagepng($this->image);
break;
}
}
public function get_width() {
return imagesx($this->image);
}
public function get_height() {
return imagesy($this->image);
}
public function resize_to_height($height) {
$ratio = $height / $this->get_height();
$width = $this->get_width() * $ratio;
$this->resize($width, $height);
}
public function resize_to_width($width) {
$ratio = $width / $this->get_width();
$height = $this->get_height() * $ratio;
$this->resize($width, $height);
}
public function scale($scale) {
$width = $this->get_width() * $scale/100;
$height = $this->get_height() * $scale/100;
$this->resize($width, $height);
}
public function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->get_width(), $this->get_height());
$this->image = $new_image;
}
}
?>
<?php
class Uploader {
private $files = array();
private $extensions = array();
private $errors = array();
private $store_directory = "./attachment";
private $resize_image_library_instance = null;
public function __construct($files) {
if (is_array($files) === false) {
$files[] = $files;
}
$this->files = $files;
}
public function set_upload_to($store_directory) {
$this->store_directory = $store_directory;
}
public function set_valid_extensions($extensions, $case_sensitive = false) {
$this->extensions = $extensions;
$this->case_sensitive = $case_sensitive;
}
public function set_resize_image_library($resize_image_library_instance) {
$this->resize_image_library_instance = $resize_image_library_instance;
}
public function is_valid_extension() {
$total = count($this->files);
for($i=0; $i<$total; $i++) {
if (empty($this->files['name'][$i]) === false) {
$file_extension = $this->get_extension($this->files['name'][$i]);
if (in_array($file_extension, $this->extensions) === false) {
$this->errors['type'] = "extension";
$this->errors['file_name'] = $this->files['name'][$i];
$this->errors['file_extension'] = $file_extension;
return false;
}
}
}
return true;
}
public function run() {
$total = count($this->files);
for($i=0; $i<$total; $i++) {
if (empty($this->files['name'][$i]) === false) {
if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$this->files['name'][$i]) == false) {
$this->errors['type'] = "run";
$this->errors['file_name'] = $this->files['name'][$i];
}
}
}
return empty($this->errors);
}
public function resize($scale_size = 50) {
$total = count($this->files);
for($i=0; $i<$total; $i++) {
$image = realpath($this->store_directory.'/'.$this->files['name'][$i]);
if (file_exists($image) === true && is_file($image) === true) {
$this->resize_image_library_instance->init($image);
$this->resize_image_library_instance->scale($scale_size);
if ($this->resize_image_library_instance->save($image) === false) {
$this->errors['type'] = "resize";
$this->errors['file_name'] = $image;
}
}
}
return empty($this->errors);
}
public function get_errors() {
return $this->errors;
}
//
private function get_extension($filename) {
$info = pathinfo($filename);
return $info['extension'];
}
private function get_filename($file) {
$info = pathinfo($file);
return $info['filename'];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment