调整图像php表单的大小

时间:2017-02-24 15:22:20

标签: php

我有两种类型的图片上传: 第一个高分辨率:

$specialImages = count($_FILES['specialImg']['name']);
        $specialMinwidth = 3000;
        $specialMinheight = 2500;
        $urlSpecialImages = array();
        if ($specialImages) {                
            for ($i=1; $i<=$specialImages; $i++) {
                if ($_FILES['specialImg']['name'][$i] != "") {                    
                    if ((($_FILES['specialImg']['type'][$i] == "image/pjpeg") || 
                        ($_FILES['specialImg']['type'][$i] == "image/jpeg") || 
                        ($_FILES['specialImg']['type'][$i] == "image/png")) && 
                        ($_FILES['specialImg']['size'][$i] < $this->return_bytes(ini_get('post_max_size')))) {

                        list($width, $height, $type, $attr) = getimagesize($_FILES['specialImg']['tmp_name'][$i]);    
                    if ($width >= $specialMinwidth  && $height >= $specialMinheight) {
                        $urlSpecialImages[$i] = $_FILES['specialImg']['tmp_name'][$i];                            
                    }
                    else {
                        $this->errors[] = $this->module->l("L'image doit être au format minimum de 3000px x 2500px", 'addproduct');
                    }

第二低分辨率:

        $images = count($_FILES['images']['name']);
        $minwidth = 1500;
        $minheight = 1500;
        if ($images <= Configuration::get('JMARKETPLACE_MAX_IMAGES')) {
            for ($i=1; $i<=Configuration::get('JMARKETPLACE_MAX_IMAGES'); $i++) {
                if ($_FILES['images']['name'][$i] != "") {
                    if ((($_FILES['images']['type'][$i] == "image/pjpeg") || 
                        ($_FILES['images']['type'][$i] == "image/jpeg") || 
                        ($_FILES['images']['type'][$i] == "image/png")) && 
                        ($_FILES['images']['size'][$i] < $this->return_bytes(ini_get('post_max_size')))) {

                        list($width, $height, $type, $attr) = getimagesize($_FILES['images']['tmp_name'][$i]);    
                    if ($width == $minwidth  && $height == $minheight) {
                       $url_images[$i] = $_FILES['images']['tmp_name'][$i];
                   }
                   else {
                    $this->errors[] = $this->module->l('The image format need to be 1500 x 1500 pixels', 'addproduct');
                }

我想使用上传的特殊$ specialImages并将其调整大小。 1500x1500图像分辨率低! 因为我希望通过一次上传获得两种分辨率!我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

图像调整大小功能示例:

function resizeImage($file = 'image.png', $maxwidth = 1366){
error_reporting(0);  
$image_info = getimagesize($file);
$image_width = $image_info[0];
$image_height = $image_info[1];
$ratio = $image_width / $maxwidth;
$info = getimagesize($file);
if ($image_width > $maxwidth) {
  // GoGoGo
  $newwidth = $maxwidth;
  $newheight = (int)($image_height / $ratio);
  if ($info['mime'] == 'image/jpeg') {    
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($file);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image_width, $image_height);
    echo imagejpeg($thumb,$file,90);
  }   
   if ($info['mime'] == 'image/jpg') {    
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($file);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $image_width, $image_height);
    echo imagejpeg($thumb,$file,90);
  }   
  if ($info['mime'] == 'image/png') {
    $im = imagecreatefrompng($file);
    $im_dest = imagecreatetruecolor($newwidth, $newheight);
    imagealphablending($im_dest, false);
    imagecopyresampled($im_dest, $im, 0, 0, 0, 0, $newwidth, $newheight, $image_width, $image_height);
    imagesavealpha($im_dest, true);
    imagepng($im_dest, $file, 9);
  }
  if ($info['mime'] == 'image/gif') {
    $im = imagecreatefromgif($file);
    $im_dest = imagecreatetruecolor($newwidth, $newheight);
    imagealphablending($im_dest, false);
    imagecopyresampled($im_dest, $im, 0, 0, 0, 0, $newwidth, $newheight, $image_width, $image_height);
    imagesavealpha($im_dest, true);
    imagegif($im_dest, $file);
  }
}}

使用:

resizeImage('D:\tmp\11.png', 1366);