Toolbox::resizePicture PHP Méthode

resizePicture() static public méthode

Resize a picture to the new size
static public resizePicture ( $source_path, $dest_path, $new_width = 71, $new_height = 71, $img_y, $img_x, $img_width, $img_height, $max_size = 500 ) : boolean
$source_path string path of the picture to be resized
$dest_path string path of the new resized picture
$new_width string new width after resized (default 71)
$new_height string new height after resized (default 71)
$img_y string y axis of picture (default 0)
$img_x string x axis of picture (default 0)
$img_width string width of picture (default 0)
$img_height string height of picture (default 0)
$max_size integer max size of the picture (default 500, is set to 0 no resize)
Résultat boolean : true or false
    static function resizePicture($source_path, $dest_path, $new_width = 71, $new_height = 71, $img_y = 0, $img_x = 0, $img_width = 0, $img_height = 0, $max_size = 500)
    {
        //get img informations (dimensions and extension)
        $img_infos = getimagesize($source_path);
        if (empty($img_width)) {
            $img_width = $img_infos[0];
        }
        if (empty($img_height)) {
            $img_height = $img_infos[1];
        }
        if (empty($new_width)) {
            $new_width = $img_infos[0];
        }
        if (empty($new_height)) {
            $new_height = $img_infos[1];
        }
        // Image max size is 500 pixels : is set to 0 no resize
        if ($max_size > 0) {
            if ($img_width > $max_size || $img_height > $max_size) {
                $source_aspect_ratio = $img_width / $img_height;
                if ($source_aspect_ratio < 1) {
                    $new_width = $max_size * $source_aspect_ratio;
                    $new_height = $max_size;
                } else {
                    $new_width = $max_size;
                    $new_height = $max_size / $source_aspect_ratio;
                }
            }
        }
        $img_type = $img_infos[2];
        switch ($img_type) {
            case IMAGETYPE_BMP:
                $source_res = imagecreatefromwbmp($source_path);
                break;
            case IMAGETYPE_GIF:
                $source_res = imagecreatefromgif($source_path);
                break;
            case IMAGETYPE_JPEG:
                $source_res = imagecreatefromjpeg($source_path);
                break;
            case IMAGETYPE_PNG:
                $source_res = imagecreatefrompng($source_path);
                break;
            default:
                return false;
        }
        //create new img resource for store thumbnail
        $source_dest = imagecreatetruecolor($new_width, $new_height);
        //resize image
        imagecopyresampled($source_dest, $source_res, 0, 0, $img_x, $img_y, $new_width, $new_height, $img_width, $img_height);
        //output img
        return imagejpeg($source_dest, $dest_path, 90);
    }

Usage Example

Exemple #1
0
 /**
  * Synchronise picture (photo) of the user
  *
  * ÷@since version 0.85
  *
  * @return string : the filename to be stored in user picture field
  **/
 function syncLdapPhoto()
 {
     if (isset($this->fields["authtype"]) && ($this->fields["authtype"] == Auth::LDAP || Auth::isAlternateAuth($this->fields['authtype']))) {
         if (isset($this->fields["id"]) && $this->fields["id"] > 0) {
             $config_ldap = new AuthLDAP();
             $ds = false;
             //connect ldap server
             if ($config_ldap->getFromDB($this->fields['auths_id'])) {
                 $ds = $config_ldap->connect();
             }
             if ($ds) {
                 //get picture fields
                 $picture_field = $config_ldap->fields['picture_field'];
                 if (empty($picture_field)) {
                     return false;
                 }
                 //get picture content in ldap
                 $info = AuthLdap::getUserByDn($ds, $this->fields['user_dn'], array($picture_field), false);
                 //getUserByDn returns an array. If the picture is empty,
                 //$info[$picture_field][0] is null
                 if (!isset($info[$picture_field][0]) || empty($info[$picture_field][0])) {
                     return "";
                 }
                 //prepare paths
                 $img = array_pop($info[$picture_field]);
                 $filename = uniqid($this->fields['id'] . '_');
                 $sub = substr($filename, -2);
                 /* 2 hex digit */
                 $file = GLPI_PICTURE_DIR . "/{$sub}/{$filename}.jpg";
                 $oldfile = GLPI_PICTURE_DIR . "/" . $this->fields["picture"];
                 // update picture if not exist or changed
                 if (!file_exists($oldfile) || sha1_file($oldfile) !== sha1($img)) {
                     if (!is_dir(GLPI_PICTURE_DIR . "/{$sub}")) {
                         mkdir(GLPI_PICTURE_DIR . "/{$sub}");
                     }
                     //save picture
                     $outjpeg = fopen($file, 'wb');
                     fwrite($outjpeg, $img);
                     fclose($outjpeg);
                     //save thumbnail
                     $thumb = GLPI_PICTURE_DIR . "/{$sub}/{$filename}_min.jpg";
                     Toolbox::resizePicture($file, $thumb);
                     return "{$sub}/{$filename}.jpg";
                 }
                 return $this->fields["picture"];
             }
         }
     }
     return false;
 }
All Usage Examples Of Toolbox::resizePicture