| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 245 |
| Code Lines | 145 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 352 | function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName = '_small', $quality = 50, $outdir = 'thumbs', $targetformat = 0) |
||
| 353 | { |
||
| 354 | require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; |
||
| 355 | |||
| 356 | global $conf,$langs; |
||
| 357 | |||
| 358 | dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat); |
||
| 359 | |||
| 360 | // Clean parameters |
||
| 361 | $file=trim($file); |
||
| 362 | |||
| 363 | // Check parameters |
||
| 364 | if (! $file) |
||
| 365 | { |
||
| 366 | // Si le fichier n'a pas ete indique |
||
| 367 | return 'ErrorBadParameters'; |
||
| 368 | } |
||
| 369 | elseif (! file_exists($file)) |
||
| 370 | { |
||
| 371 | // Si le fichier passe en parametre n'existe pas |
||
| 372 | dol_syslog($langs->trans("ErrorFileNotFound", $file), LOG_ERR); |
||
| 373 | return $langs->trans("ErrorFileNotFound", $file); |
||
| 374 | } |
||
| 375 | elseif(image_format_supported($file) < 0) |
||
| 376 | { |
||
| 377 | dol_syslog('This file '.$file.' does not seem to be an image format file name.', LOG_WARNING); |
||
| 378 | return 'ErrorBadImageFormat'; |
||
| 379 | } |
||
| 380 | elseif(!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1){ |
||
|
1 ignored issue
–
show
|
|||
| 381 | // Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0) |
||
| 382 | dol_syslog('Wrong value for parameter maxWidth', LOG_ERR); |
||
| 383 | return 'Error: Wrong value for parameter maxWidth'; |
||
| 384 | } |
||
| 385 | elseif(!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1){ |
||
|
1 ignored issue
–
show
|
|||
| 386 | // Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0) |
||
| 387 | dol_syslog('Wrong value for parameter maxHeight', LOG_ERR); |
||
| 388 | return 'Error: Wrong value for parameter maxHeight'; |
||
| 389 | } |
||
| 390 | |||
| 391 | $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image |
||
| 392 | |||
| 393 | $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image |
||
| 394 | $imgWidth = $infoImg[0]; // Largeur de l'image |
||
| 395 | $imgHeight = $infoImg[1]; // Hauteur de l'image |
||
| 396 | |||
| 397 | if ($maxWidth == -1) $maxWidth=$infoImg[0]; // If size is -1, we keep unchanged |
||
| 398 | if ($maxHeight == -1) $maxHeight=$infoImg[1]; // If size is -1, we keep unchanged |
||
| 399 | |||
| 400 | // Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette |
||
| 401 | if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) |
||
| 402 | { |
||
| 403 | // On cree toujours les vignettes |
||
| 404 | dol_syslog("File size is smaller than thumb size", LOG_DEBUG); |
||
| 405 | //return 'Le fichier '.$file.' ne necessite pas de creation de vignette'; |
||
| 406 | } |
||
| 407 | |||
| 408 | $imgfonction=''; |
||
| 409 | switch($infoImg[2]) |
||
| 410 | { |
||
| 411 | case IMAGETYPE_GIF: // 1 |
||
| 412 | $imgfonction = 'imagecreatefromgif'; |
||
| 413 | break; |
||
| 414 | case IMAGETYPE_JPEG: // 2 |
||
| 415 | $imgfonction = 'imagecreatefromjpeg'; |
||
| 416 | break; |
||
| 417 | case IMAGETYPE_PNG: // 3 |
||
| 418 | $imgfonction = 'imagecreatefrompng'; |
||
| 419 | break; |
||
| 420 | case IMAGETYPE_BMP: // 6 |
||
| 421 | // Not supported by PHP GD |
||
| 422 | break; |
||
| 423 | case IMAGETYPE_WBMP: // 15 |
||
| 424 | $imgfonction = 'imagecreatefromwbmp'; |
||
| 425 | break; |
||
| 426 | } |
||
| 427 | if ($imgfonction) |
||
| 428 | { |
||
| 429 | if (! function_exists($imgfonction)) |
||
| 430 | { |
||
| 431 | // Fonctions de conversion non presente dans ce PHP |
||
| 432 | return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | // On cree le repertoire contenant les vignettes |
||
| 437 | $dirthumb = dirname($file).($outdir?'/'.$outdir:''); // Chemin du dossier contenant les vignettes |
||
| 438 | dol_mkdir($dirthumb); |
||
| 439 | |||
| 440 | // Initialisation des variables selon l'extension de l'image |
||
| 441 | $img=null; |
||
| 442 | switch($infoImg[2]) |
||
| 443 | { |
||
| 444 | case IMAGETYPE_GIF: // 1 |
||
| 445 | $img = imagecreatefromgif($filetoread); |
||
| 446 | $extImg = '.gif'; // Extension de l'image |
||
| 447 | break; |
||
| 448 | case IMAGETYPE_JPEG: // 2 |
||
| 449 | $img = imagecreatefromjpeg($filetoread); |
||
| 450 | $extImg = (preg_match('/\.jpeg$/', $file)?'.jpeg':'.jpg'); // Extension de l'image |
||
| 451 | break; |
||
| 452 | case IMAGETYPE_PNG: // 3 |
||
| 453 | $img = imagecreatefrompng($filetoread); |
||
| 454 | $extImg = '.png'; |
||
| 455 | break; |
||
| 456 | case IMAGETYPE_BMP: // 6 |
||
| 457 | // Not supported by PHP GD |
||
| 458 | $extImg = '.bmp'; |
||
| 459 | break; |
||
| 460 | case IMAGETYPE_WBMP: // 15 |
||
| 461 | $img = imagecreatefromwbmp($filetoread); |
||
| 462 | $extImg = '.bmp'; |
||
| 463 | break; |
||
| 464 | } |
||
| 465 | if (! is_resource($img)) |
||
| 466 | { |
||
| 467 | dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING); |
||
| 468 | return 0; |
||
| 469 | } |
||
| 470 | |||
| 471 | // Initialisation des dimensions de la vignette si elles sont superieures a l'original |
||
| 472 | if($maxWidth > $imgWidth){ $maxWidth = $imgWidth; } |
||
| 473 | if($maxHeight > $imgHeight){ $maxHeight = $imgHeight; } |
||
| 474 | |||
| 475 | $whFact = $maxWidth/$maxHeight; // Facteur largeur/hauteur des dimensions max de la vignette |
||
| 476 | $imgWhFact = $imgWidth/$imgHeight; // Facteur largeur/hauteur de l'original |
||
| 477 | |||
| 478 | // Fixe les dimensions de la vignette |
||
| 479 | if($whFact < $imgWhFact) |
||
| 480 | { |
||
| 481 | // Si largeur determinante |
||
| 482 | $thumbWidth = $maxWidth; |
||
| 483 | $thumbHeight = $thumbWidth / $imgWhFact; |
||
| 484 | } |
||
| 485 | else |
||
| 486 | { |
||
| 487 | // Si hauteur determinante |
||
| 488 | $thumbHeight = $maxHeight; |
||
| 489 | $thumbWidth = $thumbHeight * $imgWhFact; |
||
| 490 | } |
||
| 491 | $thumbHeight=round($thumbHeight); |
||
| 492 | $thumbWidth=round($thumbWidth); |
||
| 493 | |||
| 494 | // Define target format |
||
| 495 | if (empty($targetformat)) $targetformat=$infoImg[2]; |
||
| 496 | |||
| 497 | // Create empty image |
||
| 498 | if ($targetformat == IMAGETYPE_GIF) |
||
| 499 | { |
||
| 500 | // Compatibilite image GIF |
||
| 501 | $imgThumb = imagecreate($thumbWidth, $thumbHeight); |
||
| 502 | } |
||
| 503 | else |
||
| 504 | { |
||
| 505 | $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight); |
||
| 506 | } |
||
| 507 | |||
| 508 | // Activate antialiasing for better quality |
||
| 509 | if (function_exists('imageantialias')) |
||
| 510 | { |
||
| 511 | imageantialias($imgThumb, true); |
||
| 512 | } |
||
| 513 | |||
| 514 | // This is to keep transparent alpha channel if exists (PHP >= 4.2) |
||
| 515 | if (function_exists('imagesavealpha')) |
||
| 516 | { |
||
| 517 | imagesavealpha($imgThumb, true); |
||
| 518 | } |
||
| 519 | |||
| 520 | // Initialisation des variables selon l'extension de l'image |
||
| 521 | // $targetformat is 0 by default, in such case, we keep original extension |
||
| 522 | switch($targetformat) |
||
| 523 | { |
||
| 524 | case IMAGETYPE_GIF: // 1 |
||
| 525 | $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF |
||
| 526 | imagecolortransparent($imgThumb, $trans_colour); |
||
| 527 | $extImgTarget = '.gif'; |
||
| 528 | $newquality='NU'; |
||
| 529 | break; |
||
| 530 | case IMAGETYPE_JPEG: // 2 |
||
| 531 | $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0); |
||
| 532 | $extImgTarget = (preg_match('/\.jpeg$/i', $file)?'.jpeg':'.jpg'); |
||
| 533 | $newquality=$quality; |
||
| 534 | break; |
||
| 535 | case IMAGETYPE_PNG: // 3 |
||
| 536 | imagealphablending($imgThumb, false); // Pour compatibilite sur certain systeme |
||
| 537 | $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel |
||
| 538 | $extImgTarget = '.png'; |
||
| 539 | $newquality=$quality-100; |
||
| 540 | $newquality=round(abs($quality-100)*9/100); |
||
| 541 | break; |
||
| 542 | case IMAGETYPE_BMP: // 6 |
||
| 543 | // Not supported by PHP GD |
||
| 544 | $extImgTarget = '.bmp'; |
||
| 545 | $newquality='NU'; |
||
| 546 | break; |
||
| 547 | case IMAGETYPE_WBMP: // 15 |
||
| 548 | $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0); |
||
| 549 | $extImgTarget = '.bmp'; |
||
| 550 | $newquality='NU'; |
||
| 551 | break; |
||
| 552 | } |
||
| 553 | if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour); |
||
| 554 | |||
| 555 | dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality"); |
||
| 556 | //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee |
||
| 557 | imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee |
||
| 558 | |||
| 559 | $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // On enleve extension quelquesoit la casse |
||
| 560 | $fileName = basename($fileName); |
||
| 561 | //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file |
||
| 562 | $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file |
||
| 563 | |||
| 564 | |||
| 565 | // Check if permission are ok |
||
| 566 | //$fp = fopen($imgThumbName, "w"); |
||
| 567 | //fclose($fp); |
||
| 568 | |||
| 569 | // Create image on disk |
||
| 570 | switch($targetformat) |
||
| 571 | { |
||
| 572 | case IMAGETYPE_GIF: // 1 |
||
| 573 | imagegif($imgThumb, $imgThumbName); |
||
| 574 | break; |
||
| 575 | case IMAGETYPE_JPEG: // 2 |
||
| 576 | imagejpeg($imgThumb, $imgThumbName, $newquality); |
||
| 577 | break; |
||
| 578 | case IMAGETYPE_PNG: // 3 |
||
| 579 | imagepng($imgThumb, $imgThumbName, $newquality); |
||
| 580 | break; |
||
| 581 | case IMAGETYPE_BMP: // 6 |
||
| 582 | // Not supported by PHP GD |
||
| 583 | break; |
||
| 584 | case IMAGETYPE_WBMP: // 15 |
||
| 585 | image2wbmp($imgThumb, $imgThumbName); |
||
| 586 | break; |
||
| 587 | } |
||
| 588 | |||
| 589 | // Set permissions on file |
||
| 590 | if (! empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK)); |
||
|
1 ignored issue
–
show
|
|||
| 591 | |||
| 592 | // Free memory. This does not delete image. |
||
| 593 | imagedestroy($img); |
||
| 594 | imagedestroy($imgThumb); |
||
| 595 | |||
| 596 | return $imgThumbName; |
||
| 597 | } |
||
| 598 |