It is given : files of jpg (jpeg) and png formats of different sizes (small, about 300Kb maximum).

Required : to form images, respecting proportions, size 110x110, and put on a white background size 120x120 (so that at least some minimum indent was).

Attempts to implement :

  1. Without checking for data type :

    public static function imageResize($out,$in,$weight,$height,$quality) { $im=imagecreatefromjpeg($in); $im1=imagecreatetruecolor($weight,$height); imagecopyresampled($im1,$im,0,0,0,0,$weight,$height,imagesx($im),imagesy($im)); imagejpeg($im1,$out,$quality); imagedestroy($im); imagedestroy($im1); } ... $path=$rootPath."/1c/img/".$product->image; $realImage=$rootPath."/products/".$product->image; ... helper::imageresize($realImage,$path,120,120,100); 

Problems : swears at png (I'm not checking the format), the output images are disproportionate, the images from the png are created just black.

  1. With verification, but without proportions :

     public static function getImage($path){ $mime = mime_content_type($path); switch($mime) { case 'image/png': $img = imagecreatefrompng($path); break; case 'image/gif': $img = imagecreatefromgif($path); break; case 'image/jpeg': $img = imagecreatefromjpeg($path); break; default: $img = null; } return $img; } ... $realImage=$rootPath."/products/".$product->image; ... $gdImage=helper::getImage($path); $im = imagecreatetruecolor(120, 120); imagejpeg($gdImage, $realImage); imagedestroy($gdImage); 

Problems : images are simply copied from one to another (with the original dimensions), images from png were copied, but stood on a black background.

That is, in the first case, I work with real images, and in the second with objects, and I don’t know how to combine both methods. In addition, you still need to create a blank white image and place on it a proportional prepared image. It is also very important that the final jpg files are exactly jpg, so that later, during the subsequent copying, I do not check for the data type (then I will need to put everything in the file in PHPExcel (but everything is ready there)).

PS For ellipsis, the getting of records from the database and the cycle in which this code is executed is omitted.

PPS @teran, thank you so much! That's what happened, thanks to your tips, maybe someone will come in handy (comments just for clarification):

 public static function imageResize($out,$in,$width,$height,$quality) { //создаСм Π½ΠΎΠ²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈΠ· Π²Ρ…ΠΎΠ΄Π½ΠΎΠ³ΠΎ с ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΎΠΉ Ρ‚ΠΈΠΏΠ° Ρ„Π°ΠΉΠ»Π° $im=self::getImage($in); //создаСм Π½ΠΎΠ²ΠΎΠ΅ пустоС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, Π·Π°Π΄Π°Π½Π½Ρ‹Ρ… Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠ² $im1=imagecreatetruecolor($width,$height); //заполняСм Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‡Ρ‚ΠΎ созданноС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ //Π±Π΅Π»Ρ‹ΠΌ Ρ†Π²Π΅Ρ‚ΠΎΠΌ (ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ создаСтся с Ρ‡Π΅Ρ€Π½Ρ‹ΠΌ Ρ„ΠΎΠ½ΠΎΠΌ) imagefill($im1,0,0,imagecolorallocate($im1, 255, 255, 255)); //ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ Π²Ρ…ΠΎΠ΄Π½ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‡Ρ‚ΠΎ созданноС ΠΈ ΠΏΠΎΠΊΡ€Π°ΡˆΠ΅Π½Π½ΠΎΠ΅ с //сохранСниСм ΠΏΡ€ΠΎΠΏΠΎΡ€Ρ†ΠΈΠΉ (сильно Π½Π΅ ΠΏΠΈΠ½Π°ΠΉΡ‚Π΅, ΠΊΠ°ΠΊ понял, Ρ‚Π°ΠΊ ΠΈ написал) imagecopyresampled($im1,$im,0,0,0,0,$width,$height,imagesx($im),imagesy($im)); //создаСм Π½ΠΎΠ²ΠΎΠ΅ JPG ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈ удаляСм экзСмпляры, //с ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌΠΈ Ρ€Π°Π±ΠΎΡ‚Π°Π»ΠΈ Π² памяти imagejpeg($im1,$out,$quality); imagedestroy($im); imagedestroy($im1); } 

PPPS Did as I managed, maybe there are more correct solutions to this problem? The only thing with which it was unsuccessful is the addition of borders along the edges of the image, that is, shrink the source up to 110x110 and place it on the canvas at 120x120, although the images are so small, but it may be useful in the future.

  • and what, you in the first code instead of imagecreatefromjpeg cannot cause the second getImage ? - teran
  • @teran hasn't tried yet, and besides, I don’t know how to png make a white background - DaemonHK
  • imagefill() is not a tricky imagefill() . teran
  • @teran thanks for the tip, try to drive the images through a series of transformations)) - DaemonHK

0