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 :
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.
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.
imagecreatefromjpegcannot cause the secondgetImage? - teranimagefill()is not a trickyimagefill(). teran