The following problem arose when working with images: images in the png format are not reduced or saved, and jpeg files are reduced and saved normally. I tried to forcibly change the extension in the file name to jpeg - it did not help, the pictures all the same do not want to be preserved and reduced. Maybe someone faced this problem?

GD Support enabled GD Version bundled (2.0.34 compatible) JPG Support enabled PNG Support enabled 

The handler itself is as follows:

 # ΠΏΡƒΡ‚ΡŒ ΠΈ имя Ρ„Π°ΠΉΠ»Π° ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $source_src = 'ΠΏΡƒΡ‚ΡŒ Π΄ΠΎ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ'; # считываСм ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ (высота/ΡˆΠΈΡ€ΠΈΠ½Π°, Ρ‚ΠΈΠΏ ΠΈ Ρ‚.ΠΏ.) # ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $params = getimagesize($source_src); # Π² зависимости ΠΎΡ‚ Ρ‚ΠΈΠΏΠ° ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ # примСняСм ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ для считывания # ΠΈ создания изобраТСния с ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ Π±ΡƒΠ΄Π΅ΠΌ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ $format = strtolower(substr($params['mime'], strpos($params['mime'], '/')+1)); $icfunc = "imagecreatefrom" . $format; $source = $icfunc($source_src); # максимально допустимый Ρ€Π°Π·ΠΌΠ΅Ρ€ # (ΠΏΠΎ ΡˆΠΈΡ€ΠΈΠ½Π΅/высотС) ΡƒΠΌΠ΅Π½ΡŒΡˆΠ΅Π½Π½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $max_size = 190; # Ссли ΡˆΠΈΡ€ΠΈΠ½Π° ΠΈΠ»ΠΈ высота ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ # большС ограничСния ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ вычислСния if ( $params[0]>$max_size || $params[1]>$max_size ) { # Π²Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ большСС: ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΈΠ»ΠΈ высота # ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ if ( $params[0]>$params[1] ) $size = $params[0]; # ΡˆΠΈΡ€ΠΈΠ½Π° else $size = $params[1]; # высота # ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ Π½Π΅Ρ…ΠΈΡ‚Ρ€ΡƒΡŽ ΠΏΡ€ΠΎΠΏΠΎΡ€Ρ†ΠΈΡŽ вычислям # ΡˆΠΈΡ€ΠΈΠ½Ρƒ ΠΈ высоту ΡƒΠΌΠ΅Π½ΡŒΡˆΠ΅Π½Π½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $nsize=getNewSize('ΠΏΡƒΡ‚ΡŒ Π΄ΠΎ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ',190,300); $resource_width = $nsize['w']; $resource_height = $nsize['h']; $resource = imagecreatetruecolor($resource_width, $resource_height); // созданиС Β«ΠΏΠΎΠ΄ΠΊΠ»Π°Π΄ΠΊΠΈΒ» # ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° ΠΈ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½ΠΎΠ³ΠΎ Π½Π° Β«ΠΏΠΎΠ΄ΠΊΠ»Π°Π΄ΠΊΡƒΒ» imagecopyresampled($resource, $source, 0, 0, 0, 0, $nsize['w'], $nsize['h'], $params[0], $params[1]); } # Ссли ΠΈΠ·ΠΌΠ΅Π½ΡΡ‚ΡŒ Ρ€Π°Π·ΠΌΠ΅Ρ€ Π½Π΅ Π½Π°Π΄ΠΎ просто присваиваСм ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ # $resource ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ else $resource = $source; //$end = end(explode('.', $picurl)); $end = 'jpeg'; imagejpeg($resource, 'Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ'.'.$end, 100); imagedestroy($resource); 

The script was taken from the public, the jipeg is processing normally, but for some reason it does not want a png ...

  • Perhaps a crookedly compiled PHP or GD library or ImageMagick, or which one is used for image processing? What is in the error logs? - Sergiks
  • The logs seem to be errors. Corrected a question. - rimlin pm


2 answers 2

I never understood why to create thumb'es to write so much code, when it can be done in one line:

 exec("convert sourceImage.jpg -thumbnail '190x300^' output.png"); 

Or so:

 $image = new Imagick('sourceImage.jpg'); $image->thumbnailImage(190, 300, true); $image->writeImage('output.png'); 

Although the first method is preferable.

PS

Crookedly assembled php, the problem with GD is facenapalm ...

Theoretically, GD can be assembled without png support.

  • Not every shared exec hosting is allowed, but thumbnails need to be done. I myself have never come across, so that GD was compiled without png, but I think if there was no png support when I started imagecreatefrompng I would swear to the log, but from the words of the TC, the log would be without errors. Well, plus PNG Support enabled kakbe hints that everything is ok :) - Sharp- eyed
  • @ Sharp-sighted, well, come across: I dealt with the situation when PNG support from standard hosting libraries --with-png-dir=... --with-jpeg-dir=... did not connect to the hosting when building its version of PHP --with-png-dir=... --with-jpeg-dir=... PNG Support enabled vehicle added after my clarification. - Sergiks

The problem is in the penultimate line in the imagejpeg function. The script has a check:

 $icfunc = "imagecreatefrom" . $format; 

those. for the png file, the imagecreatefrompng() function will be called, but you need to save the png by the imagepng() function, not imagejpeg() . You also need to save this file as *.png , not *.jpeg .

Crookedly assembled php , the problem with GD - facenapalm ...

  • Maybe there like Facebook - reducing GIF, PNG - creates JPEG from all formats? - Sergiks