September 8, 201412 yr Hello,Pls is anything wrong with my script below? when ever I upload & crop a PNG image, it comes with black background, what do I do or which crop, resize & upload script is better? <?php // Adam Khoury PHP Image Function Library 1.0 // -------------- RESIZE FUNCTION ------------- // Function for resizing any jpg, gif, or png image files function ak_img_resize($target, $newcopy, $w, $h, $ext) { list($w_orig, $h_orig) = getimagesize($target); $scale_ratio = $w_orig / $h_orig; if (($w / $h) > $scale_ratio) { $w = $h * $scale_ratio; } else { $h = $w / $scale_ratio; } $img = ""; $ext = strtolower($ext); if ($ext == "gif"){ $img = imagecreatefromgif($target); } else if($ext =="png"){ $img = imagecreatefrompng($target); } else { $img = imagecreatefromjpeg($target); } $tci = imagecreatetruecolor($w, $h); // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h) imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig); if ($ext == "gif"){ imagegif($tci, $newcopy); } else if($ext =="png"){ imagepng($tci, $newcopy); } else { imagejpeg($tci, $newcopy, 84); } } // ------------- THUMBNAIL (CROP) FUNCTION ------------- // Function for creating a true thumbnail cropping from any jpg, gif, or png image files function ak_img_thumb($target, $newcopy, $w, $h, $ext) { list($w_orig, $h_orig) = getimagesize($target); $src_x = ($w_orig / 2) - ($w / 2); $src_y = ($h_orig / 2) - ($h / 2); $ext = strtolower($ext); $img = ""; if ($ext == "gif"){ $img = imagecreatefromgif($target); } else if($ext =="png"){ $img = imagecreatefrompng($target); } else { $img = imagecreatefromjpeg($target); } $tci = imagecreatetruecolor($w, $h); imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h); if ($ext == "gif"){ imagegif($tci, $newcopy); } else if($ext =="png"){ imagepng($tci, $newcopy); } else { imagejpeg($tci, $newcopy, 84); } } ?>
September 8, 201412 yr I am not a coder but looking here imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig); Are those four zeros a colour code for black? I could be completely wrong.
September 9, 201412 yr Author Thanks Guys, @ Jack, Yes the PNG is transparent @ Fuzzy Logic, You may be right. Let me give it a check.
September 9, 201412 yr @ Jack, Yes the PNG is transparent As is 100% blank? If it's transparent I think the default for a canvas is black. I just made an image thingy and I used this guide by Salman Arshad for the crop and resample which uses the GD library and a nice method for detecting file type other than relying just on extension.
September 10, 201412 yr As is 100% blank? If it's transparent I think the default for a canvas is black. That's what I was thinking. Amending the line that appears to set a black background could work?! imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig); imagecopyresampled($tci, $img, $w, $h, $w_orig, $h_orig);
September 10, 201412 yr Aha, see this post ... the cracks are look into imagealphablending() and imagesavealpha().
October 12, 201411 yr I am not a coder but looking here Are those four zeros a colour code for black? I could be completely wrong. Those 4 zeros are for setting the destination x and y and the src x and y. However to help the OP better here is a simple class I built a while back to handle image re sizing of all image types. class imageResize { private $img; private $type; /**Method of loading an image**/ public function loadImg($file) { $info = getimagesize($file); $this->type = $info[2]; /**Switch through image types**/ switch ($this->type) { case IMAGETYPE_JPEG : $this->img = imagecreatefromjpeg($file); break; case IMAGETYPE_GIF : $this->img = imagecreatefromgif($file); break; case IMAGETYPE_PNG : $this->img = imagecreatefrompng($file); break; } } /**Image save**/ public function saveImg($file, $type = IMAGETYPE_JPEG, $com = 75) { /**Switch through image types**/ switch ($this->type) { case IMAGETYPE_JPEG : imagejpeg($this->img, $file, $com); break; case IMAGETYPE_GIF : imagegif($this->img, $file); break; case IMAGETYPE_PNG : imagepng($this->img, $file); break; } } /**Image height**/ public function imgHeight() { return imagesy($this->img); } /**Image width**/ public function imgWidth() { return imagesx($this->img); } /**Resize the image**/ public function resizeImg($w, $h) { $new = imagecreatetruecolor($w, $h); imagecopyresampled($new, $this->img, 0, 0, 0, 0, $w, $h, $this->imgWidth(), $this->imgHeight()); $this->img = $new; } } Here is an example $image = new imageResize; $image->loadImg('uploads/' . $filename);//Load the image $image->resizeImg(494, 352);//Set the width and height $image->saveImg('uploads/' . $filename);//Save the image
Create an account or sign in to comment