Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

crop & upload PNG pictures are having black background

Featured Replies

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);
}
}
?>

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.

@ 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.

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);
  • 4 weeks later...

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.