Создайте скрипт загрузки изображений PHP для переименования и изменения размера

Я использовал этот отличный сценарий для более старого проекта для загрузки изображений для использования на сайте просмотра фильмов. Как я могу настроить его, чтобы сохранить изображение как динамически сгенерированное имя, а также изменить его размер, скажем 60px x 60px? Благодаря!

$img[]='http://site.com/img.150x150.jpg'; foreach($img as $i){ save_image($i); if(getimagesize(basename($i))){ echo 'Image ' . basename($i) . ' Downloaded OK'; }else{ echo 'Image ' . basename($i) . ' Download Failed'; } } function save_image($img,$fullpath='basename'){ if($fullpath=='basename'){ $fullpath = basename($img); } $ch = curl_init ($img); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec($ch); curl_close ($ch); if(file_exists($fullpath)){ unlink($fullpath); } $fp = fopen($fullpath,'x'); fwrite($fp, $rawdata); fclose($fp); } 

Solutions Collecting From Web of "Создайте скрипт загрузки изображений PHP для переименования и изменения размера"

Класс класса resizer

 class Process{ // List of supported image formats. private $valid_ext = array( 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'wbmp' ); // Whether or not that script should continue private $halt = false; // Image Configuration array and Source Image public $errmsg = ""; var $image = array(); var $s_image; public function render($source){ $this->s_image = $source; //set the image to this class attr for other to be accessible //list the image height and width list($this->image['width'], $this->image['height']) = getimagesize($source); //get the image extension $this->image['extension'] = strtolower(preg_replace('/^.*\.([^.]+)$/D', '$1', $this->s_image)); if (!(in_array( $this->image['extension'], $this->valid_ext))){ $this->errmsg = "invalid format"; $this->halt = true; return false; //terminate the function if type format is not correct } //i think mime type should be checked instead of extension //well in that case i will do sometime //create image switch($this->image['extension']){ case 'png'; $this->image['render'] = imagecreatefrompng( $this->s_image ); imagealphablending( $this->image['render'], false ); imagesavealpha( $this->image['render'], true ); break; case 'jpg'; $this->image['render'] = imagecreatefromjpeg( $this->s_image ); break; case 'jpeg'; $this->image['render'] = imagecreatefromjpeg( $this->s_image ); break; case 'gif'; $this->image['render'] = imagecreatefromgif( $this->s_image ); break; case 'bmp'; $this->image['render'] = imagecreatefromwbmp( $this->s_image ); break; case 'wbmp'; $this->image['render'] = imagecreatefromwbmp( $this->s_image ); break; default: $this->errmsg = "invalid format"; $this->halt = true; return false; break; } } public function contrain($width, $height){ if(!($this->halt)){ if($this->image['extension'] == 'gif'){ $this->image['composite'] = imagecreatetruecolor($width, $height); imagecopyresample($this->image['composite'], $this->image['render'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height'] ); $this->image['colorcount'] = imagecolorstotal( $this->image['render'] ); imagetruecolortopalette( $this->image['composite'], true, $this->image['colorcount'] ); imagepalettecopy( $this->image['composite'], $this->image['render'] ); $this->image['transparentcolor'] = imagecolortransparent( $this->image['render'] ); imagefill( $this->image['composite'], 0, 0, $this->image['transparentcolor'] ); imagecolortransparent( $this->image['composite'], $this->image['transparentcolor'] ); } else { $this->image['composite'] = imagecreatetruecolor($width, $height); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height'] ); } }else{ $this->halt = true; $this->errmsg = "Execution halted"; return false; } } public function proportion($max_width, $max_height){ if (!( $this->halt)){ if($this->image['extension'] == 'gif'){ $this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width / $this->image['width'] : $max_height/$this->image['height']; if( $this->image['width'] > $max_width || $this->image['height'] > $max_height ) { $new_width = $this->image['width'] * $this->image['ratio']; $new_height = $this->image['height'] * $this->image['ratio']; }else{ $new_width = $this->image['width']; $new_height = $this->image['height']; } $this->image['composite'] = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $new_width, $new_height, $this->image['width'], $this->image['height'] ); $this->image['colorcount'] = imagecolorstotal( $this->image['render'] ); imagetruecolortopalette( $this->image['composite'], true, $this->image['colorcount'] ); imagepalettecopy( $this->image['composite'], $this->image['render'] ); $this->image['transparentcolor'] = imagecolortransparent( $this->image['render'] ); imagefill( $this->image['composite'], 0, 0, $this->image['transparentcolor'] ); imagecolortransparent( $this->image['composite'], $this->image['transparentcolor'] ); }else{ $this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width / $this->image['width'] : $max_height/$this->image['height']; if( $this->image['width'] > $max_width || $this->image['height'] > $max_height ) { $new_width = $this->image['width'] * $this->image['ratio']; $new_height = $this->image['height'] * $this->image['ratio']; }else{ $new_width = $this->image['width']; $new_height = $this->image['height']; } $this->image['composite'] = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $new_width, $new_height, $this->image['width'], $this->image['height'] ); } } else { $this->errmsg = "Execution halted"; $this->halt = true; return false; } } public function output($quality = 100){ if(!(is_numeric($quality))){ $quality = 100; } if(!($this->halt)){ switch($this->image['extension']){ case 'png'; header('Content-Type: image/png'); imagepng($this->image['composite'], null, null ); break; case 'jpg'; header('Content-Type: image/jpeg'); imagejpeg($this->image['composite'], null, $quality); break; case 'jpeg'; header( 'Content-Type: image/jpeg' ); imagejpeg($this->image['composite'], null, $quality); break; case 'gif'; header( 'Content-Type: image/gif' ); imagegif($this->image['composite'], null, $quality); break; case 'bmp'; header('Content-Type: image/wbmp' ); imagewbmp($this->image['composite'], null, null); break; case 'wbmp'; header('Content-Type: image/wbmp' ); imagewbmp($this->image['composite'], null, null); break; default: $this->errmsg = "invalid format"; return false; break; } }else{ $this->errmsg = "Execution halted"; return false; } } public function saveto($destination, $filename, $quality = 100){ if(!(is_numeric($quality))){ $quality = 100; } if(!($this->halt)){ switch($this->image['extension']){ case 'png'; imagepng($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; case 'jpg'; imagejpeg($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'jpeg'; imagejpeg($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'gif'; imagegif($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'bmp'; imagewbmp($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; case 'wbmp'; imagewbmp($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; default: $this->errmsg = "invalid format"; return false; break; } }else{ $this->errmsg = "Execution halted"; return false; } } public function clear_cache(){ imagedestroy( $this->image['composite'] ); imagedestroy( $this->image['render'] ); unset($this->image); unset($this->s_image); $this->halt = false; } } с class Process{ // List of supported image formats. private $valid_ext = array( 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'wbmp' ); // Whether or not that script should continue private $halt = false; // Image Configuration array and Source Image public $errmsg = ""; var $image = array(); var $s_image; public function render($source){ $this->s_image = $source; //set the image to this class attr for other to be accessible //list the image height and width list($this->image['width'], $this->image['height']) = getimagesize($source); //get the image extension $this->image['extension'] = strtolower(preg_replace('/^.*\.([^.]+)$/D', '$1', $this->s_image)); if (!(in_array( $this->image['extension'], $this->valid_ext))){ $this->errmsg = "invalid format"; $this->halt = true; return false; //terminate the function if type format is not correct } //i think mime type should be checked instead of extension //well in that case i will do sometime //create image switch($this->image['extension']){ case 'png'; $this->image['render'] = imagecreatefrompng( $this->s_image ); imagealphablending( $this->image['render'], false ); imagesavealpha( $this->image['render'], true ); break; case 'jpg'; $this->image['render'] = imagecreatefromjpeg( $this->s_image ); break; case 'jpeg'; $this->image['render'] = imagecreatefromjpeg( $this->s_image ); break; case 'gif'; $this->image['render'] = imagecreatefromgif( $this->s_image ); break; case 'bmp'; $this->image['render'] = imagecreatefromwbmp( $this->s_image ); break; case 'wbmp'; $this->image['render'] = imagecreatefromwbmp( $this->s_image ); break; default: $this->errmsg = "invalid format"; $this->halt = true; return false; break; } } public function contrain($width, $height){ if(!($this->halt)){ if($this->image['extension'] == 'gif'){ $this->image['composite'] = imagecreatetruecolor($width, $height); imagecopyresample($this->image['composite'], $this->image['render'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height'] ); $this->image['colorcount'] = imagecolorstotal( $this->image['render'] ); imagetruecolortopalette( $this->image['composite'], true, $this->image['colorcount'] ); imagepalettecopy( $this->image['composite'], $this->image['render'] ); $this->image['transparentcolor'] = imagecolortransparent( $this->image['render'] ); imagefill( $this->image['composite'], 0, 0, $this->image['transparentcolor'] ); imagecolortransparent( $this->image['composite'], $this->image['transparentcolor'] ); } else { $this->image['composite'] = imagecreatetruecolor($width, $height); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height'] ); } }else{ $this->halt = true; $this->errmsg = "Execution halted"; return false; } } public function proportion($max_width, $max_height){ if (!( $this->halt)){ if($this->image['extension'] == 'gif'){ $this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width / $this->image['width'] : $max_height/$this->image['height']; if( $this->image['width'] > $max_width || $this->image['height'] > $max_height ) { $new_width = $this->image['width'] * $this->image['ratio']; $new_height = $this->image['height'] * $this->image['ratio']; }else{ $new_width = $this->image['width']; $new_height = $this->image['height']; } $this->image['composite'] = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $new_width, $new_height, $this->image['width'], $this->image['height'] ); $this->image['colorcount'] = imagecolorstotal( $this->image['render'] ); imagetruecolortopalette( $this->image['composite'], true, $this->image['colorcount'] ); imagepalettecopy( $this->image['composite'], $this->image['render'] ); $this->image['transparentcolor'] = imagecolortransparent( $this->image['render'] ); imagefill( $this->image['composite'], 0, 0, $this->image['transparentcolor'] ); imagecolortransparent( $this->image['composite'], $this->image['transparentcolor'] ); }else{ $this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width / $this->image['width'] : $max_height/$this->image['height']; if( $this->image['width'] > $max_width || $this->image['height'] > $max_height ) { $new_width = $this->image['width'] * $this->image['ratio']; $new_height = $this->image['height'] * $this->image['ratio']; }else{ $new_width = $this->image['width']; $new_height = $this->image['height']; } $this->image['composite'] = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $new_width, $new_height, $this->image['width'], $this->image['height'] ); } } else { $this->errmsg = "Execution halted"; $this->halt = true; return false; } } public function output($quality = 100){ if(!(is_numeric($quality))){ $quality = 100; } if(!($this->halt)){ switch($this->image['extension']){ case 'png'; header('Content-Type: image/png'); imagepng($this->image['composite'], null, null ); break; case 'jpg'; header('Content-Type: image/jpeg'); imagejpeg($this->image['composite'], null, $quality); break; case 'jpeg'; header( 'Content-Type: image/jpeg' ); imagejpeg($this->image['composite'], null, $quality); break; case 'gif'; header( 'Content-Type: image/gif' ); imagegif($this->image['composite'], null, $quality); break; case 'bmp'; header('Content-Type: image/wbmp' ); imagewbmp($this->image['composite'], null, null); break; case 'wbmp'; header('Content-Type: image/wbmp' ); imagewbmp($this->image['composite'], null, null); break; default: $this->errmsg = "invalid format"; return false; break; } }else{ $this->errmsg = "Execution halted"; return false; } } public function saveto($destination, $filename, $quality = 100){ if(!(is_numeric($quality))){ $quality = 100; } if(!($this->halt)){ switch($this->image['extension']){ case 'png'; imagepng($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; case 'jpg'; imagejpeg($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'jpeg'; imagejpeg($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'gif'; imagegif($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'bmp'; imagewbmp($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; case 'wbmp'; imagewbmp($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; default: $this->errmsg = "invalid format"; return false; break; } }else{ $this->errmsg = "Execution halted"; return false; } } public function clear_cache(){ imagedestroy( $this->image['composite'] ); imagedestroy( $this->image['render'] ); unset($this->image); unset($this->s_image); $this->halt = false; } } с class Process{ // List of supported image formats. private $valid_ext = array( 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'wbmp' ); // Whether or not that script should continue private $halt = false; // Image Configuration array and Source Image public $errmsg = ""; var $image = array(); var $s_image; public function render($source){ $this->s_image = $source; //set the image to this class attr for other to be accessible //list the image height and width list($this->image['width'], $this->image['height']) = getimagesize($source); //get the image extension $this->image['extension'] = strtolower(preg_replace('/^.*\.([^.]+)$/D', '$1', $this->s_image)); if (!(in_array( $this->image['extension'], $this->valid_ext))){ $this->errmsg = "invalid format"; $this->halt = true; return false; //terminate the function if type format is not correct } //i think mime type should be checked instead of extension //well in that case i will do sometime //create image switch($this->image['extension']){ case 'png'; $this->image['render'] = imagecreatefrompng( $this->s_image ); imagealphablending( $this->image['render'], false ); imagesavealpha( $this->image['render'], true ); break; case 'jpg'; $this->image['render'] = imagecreatefromjpeg( $this->s_image ); break; case 'jpeg'; $this->image['render'] = imagecreatefromjpeg( $this->s_image ); break; case 'gif'; $this->image['render'] = imagecreatefromgif( $this->s_image ); break; case 'bmp'; $this->image['render'] = imagecreatefromwbmp( $this->s_image ); break; case 'wbmp'; $this->image['render'] = imagecreatefromwbmp( $this->s_image ); break; default: $this->errmsg = "invalid format"; $this->halt = true; return false; break; } } public function contrain($width, $height){ if(!($this->halt)){ if($this->image['extension'] == 'gif'){ $this->image['composite'] = imagecreatetruecolor($width, $height); imagecopyresample($this->image['composite'], $this->image['render'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height'] ); $this->image['colorcount'] = imagecolorstotal( $this->image['render'] ); imagetruecolortopalette( $this->image['composite'], true, $this->image['colorcount'] ); imagepalettecopy( $this->image['composite'], $this->image['render'] ); $this->image['transparentcolor'] = imagecolortransparent( $this->image['render'] ); imagefill( $this->image['composite'], 0, 0, $this->image['transparentcolor'] ); imagecolortransparent( $this->image['composite'], $this->image['transparentcolor'] ); } else { $this->image['composite'] = imagecreatetruecolor($width, $height); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height'] ); } }else{ $this->halt = true; $this->errmsg = "Execution halted"; return false; } } public function proportion($max_width, $max_height){ if (!( $this->halt)){ if($this->image['extension'] == 'gif'){ $this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width / $this->image['width'] : $max_height/$this->image['height']; if( $this->image['width'] > $max_width || $this->image['height'] > $max_height ) { $new_width = $this->image['width'] * $this->image['ratio']; $new_height = $this->image['height'] * $this->image['ratio']; }else{ $new_width = $this->image['width']; $new_height = $this->image['height']; } $this->image['composite'] = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $new_width, $new_height, $this->image['width'], $this->image['height'] ); $this->image['colorcount'] = imagecolorstotal( $this->image['render'] ); imagetruecolortopalette( $this->image['composite'], true, $this->image['colorcount'] ); imagepalettecopy( $this->image['composite'], $this->image['render'] ); $this->image['transparentcolor'] = imagecolortransparent( $this->image['render'] ); imagefill( $this->image['composite'], 0, 0, $this->image['transparentcolor'] ); imagecolortransparent( $this->image['composite'], $this->image['transparentcolor'] ); }else{ $this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width / $this->image['width'] : $max_height/$this->image['height']; if( $this->image['width'] > $max_width || $this->image['height'] > $max_height ) { $new_width = $this->image['width'] * $this->image['ratio']; $new_height = $this->image['height'] * $this->image['ratio']; }else{ $new_width = $this->image['width']; $new_height = $this->image['height']; } $this->image['composite'] = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $this->image['composite'], $this->image['render'], 0, 0, 0, 0, $new_width, $new_height, $this->image['width'], $this->image['height'] ); } } else { $this->errmsg = "Execution halted"; $this->halt = true; return false; } } public function output($quality = 100){ if(!(is_numeric($quality))){ $quality = 100; } if(!($this->halt)){ switch($this->image['extension']){ case 'png'; header('Content-Type: image/png'); imagepng($this->image['composite'], null, null ); break; case 'jpg'; header('Content-Type: image/jpeg'); imagejpeg($this->image['composite'], null, $quality); break; case 'jpeg'; header( 'Content-Type: image/jpeg' ); imagejpeg($this->image['composite'], null, $quality); break; case 'gif'; header( 'Content-Type: image/gif' ); imagegif($this->image['composite'], null, $quality); break; case 'bmp'; header('Content-Type: image/wbmp' ); imagewbmp($this->image['composite'], null, null); break; case 'wbmp'; header('Content-Type: image/wbmp' ); imagewbmp($this->image['composite'], null, null); break; default: $this->errmsg = "invalid format"; return false; break; } }else{ $this->errmsg = "Execution halted"; return false; } } public function saveto($destination, $filename, $quality = 100){ if(!(is_numeric($quality))){ $quality = 100; } if(!($this->halt)){ switch($this->image['extension']){ case 'png'; imagepng($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; case 'jpg'; imagejpeg($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'jpeg'; imagejpeg($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'gif'; imagegif($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, $quality ); break; case 'bmp'; imagewbmp($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; case 'wbmp'; imagewbmp($this->image['composite'], $destination.DIRECTORY_SEPARATOR.$filename, null); break; default: $this->errmsg = "invalid format"; return false; break; } }else{ $this->errmsg = "Execution halted"; return false; } } public function clear_cache(){ imagedestroy( $this->image['composite'] ); imagedestroy( $this->image['render'] ); unset($this->image); unset($this->s_image); $this->halt = false; } } 

И использование

 $process = new Process(); $process->render($filepath); $process->proportion($width, $height); $process->saveto($upload_path, $filename, $quality); //$quality is in % 

Взгляните на пример функции imagecopyresized .

Сохраните изображение в папке, например Cache. Если вы выполнили функцию save_image() , выполните функцию, которая изменяет размер изображения и сохраняет изображение под новым именем. Затем вы удаляете изображение из папки cache .