Uploading PNG to Website Server - php

I have seen loads of other topics and I've tried them all. Can someone please help with why this script won't upload PNG files? Blank PNG image being displayed.
$image = $_FILES['file']['tmp_name'];
$image_name = $_FILES['file']['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$location = "Profiles/{$user}/Picture/{$image_name}";
$new_image = imagecreatetruecolor(100, 100);
$source_image = imagecreatefrompng($image);
imagealphablending($source_image, false);
imagesavealpha($source_image, true);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, 100, 100, $image_width, $image_height);
imagepng($new_image, '../' . $location, 9);

You're not declaring $image_width or $image-height and you are referencing $image instead of $source_image in imagecopyresampled().
I too was getting a plain white image, but after this I get the expected result:
$image = $_FILES['file']['tmp_name'];
$image_name = $_FILES['file']['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$location = "Profiles/{$user}/Picture/{$image_name}";
$new_image = imagecreatetruecolor(100, 100);
$source_image = imagecreatefrompng($image);
// Get the width & height of the uploaded image.
$image_width = imagesx($source_image);
$image_height = imagesy($source_image);
imagealphablending($source_image, false);
imagesavealpha($source_image, true);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $source_image, 0, 0, 0, 0, 100, 100, $image_width, $image_height);
imagepng($new_image, '../' . $location, 9);

Related

PHP - Resizing and saving PNG images. Some are all black

I'm resizing and saving PNGs with PHP. The first example doesn't work, the PNG ends up being completely black, but the second example works. The first example doesn't have a transparent background but the second one does. Why doesn't the first example work? Is it a problem with the URL?
<?php
$id = "example";
//$originalFile = "https://th.bing.com/th/id/OIP.v_YT6iKMW6sOOVdCxVYQkwHaE8?pid=ImgDet&rs=1.png"; // Doesn't work
$originalFile = "https://cdn.globalxetfs.com/content/files/210618-China_Materials_02.png"; // Works
list($originalWidth, $originalHeight) = getimagesize($originalFile);
$originalImage = #imagecreatefrompng($originalFile);
$newHeight = 255;
$newWidth = 255;
// Create empty canvas
$resizedImage = "";
$resizedImage = imagecreatetruecolor($newWidth, $newHeight); // width, height
// Preserve transparency
imagesavealpha($resizedImage, true);
$color = imagecolorallocatealpha($resizedImage, 0, 0, 0, 127);
imagefill($resizedImage, 0, 0, $color);
// Resize image
imagecopyresampled(
$resizedImage, $originalImage, 0, 0, 0, 0,
$newWidth, $newHeight, $originalWidth, $originalHeight
);
header('Content-type: image/png');
$imageName = $id;
$imageName = $imageName . ".png";
if(imagepng($resizedImage, "images/" . $imageName)){
echo "Image uploaded";
}
?>
The "first example" actually is a JPG file (after the server image rendering) , so you need to use
#imagecreatefromjpeg instead of #imagecreatefrompng
So the codes (working) should be:
<?php
$id = "example";
$originalFile = "https://th.bing.com/th/id/OIP.v_YT6iKMW6sOOVdCxVYQkwHaE8?pid=ImgDet&rs=1.png";
//$originalFile = "https://cdn.globalxetfs.com/content/files/210618-China_Materials_02.png";
list($originalWidth, $originalHeight) = getimagesize($originalFile);
$originalImage = #imagecreatefromjpeg($originalFile);
$newHeight = 255;
$newWidth = 255;
// Create empty canvas
$resizedImage = "";
$resizedImage = imagecreatetruecolor($newWidth, $newHeight); // width, height
// Preserve transparency
imagesavealpha($resizedImage, true);
$color = imagecolorallocatealpha($resizedImage, 0, 0, 0, 127);
imagefill($resizedImage, 0, 0, $color);
// Resize image
imagecopyresampled(
$resizedImage, $originalImage, 0, 0, 0, 0,
$newWidth, $newHeight, $originalWidth, $originalHeight
);
header('Content-type: image/png');
//$imageName = $id;
//$imageName = $imageName . ".png";
//if(imagepng($resizedImage, "images/" . $imageName)){
// echo "Image uploaded";
//}
imagepng($resizedImage);
?>

How to create retina image from JPEG with PHP

A few months ago i wrote the following script to convert an uploaded image with PHP to Retina and non retina images. The iphone app that was working with this script only used PNG images, so i wrote the script to work with PNG's.
$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.png', '_retina.png', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));
$image_info = getimagesize($filename);
$image = imagecreatefrompng($filename);
$width = imagesx($image);
$height = imagesy($image);
$new_width = $width/2.0;
$new_height = $height/2.0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$new_filename = str_replace('_retina.png', '.png', $filename);
imagepng($new_image, $new_filename);
Now i need the same script but then to be used with Jpeg images. Because the iphone app will load images with a higher resolution we chose Jpeg. But i can't figure out how to make that work.
What i've tried so far:
Replacing imagecreatefrompng with the jpeg version
Replacing imagepng with the jpeg version
Does anybody have a working example or useful link that can set me to the right direction?
I figured out what the problem was about. I assumed jpg php functions could not handle the transparency, so i removed those lines and forgot about them. Apparently it just creates a white background and it does not fail. So the script is as follows:
$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.jpg', '_retina.jpg', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));
$image_info = getimagesize($filename);
$image = imagecreatefromjpeg($filename);
$width = imagesx($image);
$height = imagesy($image);
$new_width = $width/2.0;
$new_height = $height/2.0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$new_filename = str_replace('_retina.jpg', '.jpg', $filename);
imagejpeg($new_image, $new_filename);

Image resampling script for png and gifs

I have this PHP script that receives an uploaded image. The uploaded image is saved in a temp folder, and then this script resamples the image and saves it to the correct folder. A user can upload either JPG, PNG or GIF files. This script only caters for JPG files though.
How would I modify this script to resize both PNG's and GIF's without losing transparency?
$targ_w = $targ_h = 150;
$jpeg_quality = 90;
$src = $_POST['n'];
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
$new_src = str_replace('/temp','',$_POST['n']);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
imagejpeg($dst_r,$new_src,$jpeg_quality);
JPEG images can't have transparent background.
Instead you can make the image based on imagesavealpha():
$targ_w = $targ_h = 150;
$newImage = imagecreatetruecolor($targ_w, $targ_h);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
$transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagefilledrectangle($newImage, 0, 0, $targ_w, $targ_h, $transparent);
$src = $_POST['n'];
$img_r = imagecreatefromstring(file_get_contents($src));
$img_r_size = getimagesize($src);
$width_r = $img_r_size[0];
$height_r = $img_r_size[1];
if($width_r > $height_r){
$width_ratio = $targ_w / $width_r;
$new_width = $targ_w;
$new_height = $height_r * $width_ratio;
} else {
$height_ratio = $targ_h / $height_r;
$new_width = $width_r * $height_ratio;
$new_height = $targ_h;
}
imagecopyresampled($newImage, $img_r, 0, 0, 0, 0, $new_width, $new_height, $width_r, $height_r);
$new_src = str_replace('/temp','',$_POST['n']);
imagepng($newImage, $new_src);
It will make a PNG from both PNG and GIF (that have transparent background, and resize to 150x150.
This is just an example, as it does not constrain proportions.
I had this problem few months ago and solved it by using code below:
imagealphablending($target_image, false);
imagesavealpha($target_image, true);

Resize PNG image in PHP

I'm getting a no image display when resizing PNG however the following code works for JPEG.
list($width_orig, $height_orig) = getimagesize( $fileName );
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
if( $type )){
switch( $type ){
case 'image/jpeg':
$image = imagecreatefromjpeg($fileName);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
break;
case 'image/png':
imagealphablending( $image_p, false );
imagesavealpha( $image_p, true );
$image = imagecreatefrompng( $fileName );
imagecopyresampled( $image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagepng($image_p, null, 100);
break;
}
}
I've put the headers in but for some reason I'm doing something wrong for png images.
Last argument in imagepng($image_p, null, 100) should be between 0 and 9.
try this:
$image = imagecreatefrompng ( $filename );
$new_image = imagecreatetruecolor ( $width, $height ); // new wigth and height
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
imagecopyresampled ( $new_image, $image, 0, 0, 0, 0, $width, $height, imagesx ( $image ), imagesy ( $image ) );
$image = $new_image;
// saving
imagealphablending($image , false);
imagesavealpha($image , true);
imagepng ( $image, $filename );
see if this works
#upload de image
public function upImagem($imagem, $dir, $res, $id, $tam){
$arquivo = $imagem;
$arq_nome = $arquivo['name'];
$ext = $arquivo['type'];
$nome=$this->RenImg($arquivo, $dir, $id, $tam);
$imagem = $arquivo['tmp_name'];
if($ext=='image/jpeg'){
$img = imagecreatefromjpeg($imagem);
}
elseif($ext=='image/png'){
$img = imagecreatefrompng($imagem);
}
elseif($ext=='image/gif'){
$img = imagecreatefromgif($imagem);
}
if(($ext=='image/png') or ($ext=='image/gif')){
list($x, $y) = getimagesize($arquivo['tmp_name']);
}
elseif($ext=='image/jpeg'){
$x = imagesx($img);//original height
$y = imagesy($img);//original width
}
$altura = $res[1];
$largura = $res[0];
$nova = imagecreatetruecolor($largura,$altura);
$preto = imagecolorallocate($nova, 0, 0, 0);
if(($ext=='image/png') or ($ext=='image/gif')){
imagealphablending($nova , false);
imagesavealpha($nova , true);
}
if($ext=='image/png'){
imagecolortransparent ($nova, $preto);
imagecopymerge($img, $nova, 0, 0, 0, 0, imagesx($nova), imagesy($nova), 100);
imagecopyresized($nova,$img,0,0,0,0,$largura,$altura, $x, $y );
}
else {
imagecopyresampled($nova,$img,0,0,0,0,$largura,$altura,$x,$y);
}
if($ext=='image/jpeg'){
imagejpeg($nova,$nome,99);
}
elseif($ext=='image/gif'){
imagealphablending($nova , false);
imagesavealpha($nova , true);
imagegif($nova,$nome,99);
}
elseif($ext=='image/png'){
imagealphablending($nova , false);
imagesavealpha($nova , true);
imagepng($nova,$nome);
}
imagedestroy($img);
imagedestroy($nova);
}
#renames the image
public function RenImg($arq,$dir,$id,$tam){
$arq_nome = $arq['name'];
$arq_nome2=str_replace('.jpg','',$arq['name']);//renomeia o arquivo
$arq_nome2=str_replace('.png','',$arq_nome2);//renomeia o arquivo
$arq_nome2=str_replace('.gif','',$arq_nome2);//renomeia o arquivo
//$new_name = md5($arq_nome);
$ext = $this->getExt($arq_nome);
$nome = $dir.$id.$tam.'.jpg';//.'.'.$ext
return $nome;
}
#capture the file extension
public function getExt($arq){
$ext = pathinfo($arq, PATHINFO_EXTENSION);
return $ext;
}
This is a code i use for JPG/PNG rezise.
$imagename = "default";
if (isset ($_FILES['arquivo'])) {
$imagename = $imagename . ".jpg";
$source = $_FILES['arquivo']['tmp_name'];
$target = "images/tmp/" . $imagename;
$type = $_FILES["arquivo"]["type"];
//JPG or JPEG
if ($type == "image/jpeg" || $type == "image/jpg") {
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "images/" . $imagepath; //Path to save the image
$file = "images/tmp/" . $imagepath; //path to orginal size image
list($width, $height) = getimagesize($file);
$modwidth = 1920;
$diff = $width / $modwidth; // Use $modheight = $idff to mantain aspect ratio
$modheight = 1080;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
echo "<center><b><h5>Image was updated!</h5></b>";
imagejpeg($tn, $save, 100);
} elseif ($type == "image/png") { //PNG
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "images/" . $imagepath; //Path to save the image
$file = "images/tmp/" . $imagepath; //path to orginal size image
list($width, $height) = getimagesize($file);
$modwidth = 1920;
$diff = $width / $modwidth; // Use $modheight = $idff to mantain aspect ratio
$modheight = 1080;
$tn = imagecreatetruecolor($modwidth, $modheight);
imagealphablending($tn, false);
imagesavealpha($tn, true);
$image = imagecreatefrompng($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
echo "Image was updated!";
imagepng($tn, $save, 9);
} else {
echo "Error!";
}
}

How do I resize pngs with transparency in PHP?

I'm attempting to resize pngs with transparent backgrounds in PHP and the code samples I've found online don't work for me. Here's the code I'm using, advice will be much appreciated!
$this->image = imagecreatefrompng($filename);
imagesavealpha($this->image, true);
$newImage = imagecreatetruecolor($width, $height);
// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $newImage;
imagepng($this->image,$filename);
Update
By 'not working' I meant to say the background color changes to black when I resize pngs.
From what I can tell, you need to set the blending mode to false, and the save alpha channel flag to true before you do the imagecolorallocatealpha()
<?php
/**
* https://stackoverflow.com/a/279310/470749
*
* #param resource $image
* #param int $newWidth
* #param int $newHeight
* #return resource
*/
public function getImageResized($image, int $newWidth, int $newHeight) {
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($newImg, false);
imagesavealpha($newImg, true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $newWidth, $newHeight, $transparent);
$src_w = imagesx($image);
$src_h = imagesy($image);
imagecopyresampled($newImg, $image, 0, 0, 0, 0, $newWidth, $newHeight, $src_w, $src_h);
return $newImg;
}
?>
UPDATE : This code is working only on background transparent with opacity = 0. If your image have 0 < opacity < 100 it'll be black background.
Here is a final solution that is working fine for me.
function resizePng($im, $dst_width, $dst_height) {
$width = imagesx($im);
$height = imagesy($im);
$newImg = imagecreatetruecolor($dst_width, $dst_height);
imagealphablending($newImg, false);
imagesavealpha($newImg, true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $width, $height, $transparent);
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);
return $newImg;
}
using imagescale is better compared to imagecopyresampled. No empty image resource required for the resized image, requires only two arguments compared to the ten required by imagecopyresampled. Also produces better quality with smaller sizes. If using PHP 5.5.18 or earlier, or PHP 5.6.2 or earlier, you should provide the height which is the 3rd argument as the aspect ratio calculation was incorrect.
$this->image = imagecreatefrompng($filename);
$scaled = imagescale($this->image, $width);
imagealphablending($scaled, false);
imagesavealpha($scaled, true);
imagepng($scaled, $filename);
The filling of the new image with a transparent colour is also required (as Dycey coded but I'm guessing forgot to mention :)), not just the 'strategic' saving by itself.
IIRC, you also need to be sure PNGs are 24bit, ie truecolor, and not 8bit to avoid buggy behaviour.
old thread, but just in case - Dycey's example should work, if you name things correctly. Here is a modified version used in my image resizing class. Notice the check to make sure imagecolorallocatealpha() is defined, which it won't be if you are using GD <2.0.8
/**
* usually when people use PNGs, it's because they need alpha channel
* support (that means transparency kids). So here we jump through some
* hoops to create a big transparent rectangle which the resampled image
* will be copied on top of. This will prevent GD from using its default
* background, which is black, and almost never correct. Why GD doesn't do
* this automatically, is a good question.
*
* #param $w int width of target image
* #param $h int height of target image
* #return void
* #private
*/
function _preallocate_transparency($w, $h) {
if (!empty($this->filetype) && !empty($this->new_img) && $this->filetype == 'image/png')) {
if (function_exists('imagecolorallocatealpha')) {
imagealphablending($this->new_img, false);
imagesavealpha($this->new_img, true);
$transparent = imagecolorallocatealpha($this->new_img, 255, 255, 255, 127);
imagefilledrectangle($this->new_img, 0, 0, $tw, $th, $transparent);
}
}
}
It's probably related to the newer versions of PHP (I tested with PHP 5.6) but this now works without the need to fill the image with a transparent background:
$image_p = imagecreatetruecolor(480, 270);
imageAlphaBlending($image_p, false);
imageSaveAlpha($image_p, true);
$image = imagecreatefrompng('image_with_some_transaprency.png');
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 480, 270, 1920, 1080);
imagepng($image_p, 'resized.png', 0);
this is also not working for me :(
thisis my solution.. but i also get a black background and the image is not transparent
<?php
$img_id = 153;
$source = "images/".$img_id.".png";
$source = imagecreatefrompng($source);
$o_w = imagesx($source);
$o_h = imagesy($source);
$w = 200;
$h = 200;
$newImg = imagecreatetruecolor($w, $h);
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $w, $h, $transparent);
imagecopyresampled($newImg, $source, 0, 0, 0, 0, $w, $h, $o_w, $o_h);
imagepng($newImg, $img_id.".png");
?>
<img src="<?php echo $img_id.".png" ?>" />
Here is full code working for png files with preserving their images transparency..
list($width, $height) = getimagesize($filepath);
$new_width = "300";
$new_height = "100";
if($width>$new_width && $height>$new_height)
{
$image_p = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
$image = imagecreatefrompng($filepath);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p,$filepath,5);
}
Full example. Notice that for some png images found on internet it works incorrect, but for my own created with photoshop it works fine.
header('Content-Type: image/png');
$filename = "url to some image";
$newWidth = 300;
$newHeight = 300;
$imageInfo = getimagesize($filename);
$image = imagecreatefrompng($filename); //create source image resource
imagesavealpha($image, true); //saving transparency
$newImg = imagecreatetruecolor($newWidth, $newHeight); //creating conteiner for new image
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); //seting transparent background
imagefilledrectangle($newImg, 0, 0, $newWidth, $newHeight, $transparent);
imagecopyresampled($newImg, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imageInfo[0], $imageInfo[1]);
imagepng($newImg); //printout image string
You can simply resize the image with imagescale()
This code works fine for me:
$image = imagescale($image, 720, -1, IMG_BICUBIC);
imagepng($image);
Nor the above solution worked for me. This is the way what i found out to solve the issue.
// upload directory
$upload_dir = "../uploads/";
// valid image formats
$valid_formats = array("jpg", "jpeg", "png");
// maximum image size 1 mb
$max_size = 1048576;
// crop image width, height
$nw = $nh = 800;
$nw1 = $nh1 = 400;
$nw3 = $nh3 = 200;
$nw2 = $nh2 = 100;
// checks that if upload_dir a directory/not
if (is_dir($upload_dir) && is_writeable($upload_dir)) {
// not empty file
if (!empty($_FILES['image'])) {
// assign file name
$name = $_FILES['image']['name'];
// $_FILES to execute all files within a loop
if ($_FILES['image']['error'] == 4) {
$message = "Empty FIle";
}
if ($_FILES['image']['error'] == 0) {
if ($_FILES['image']['size'] > $max_size) {
echo "E-Image is too large!<br>";
$_SESSION['alert'] = "Image is too large!!";
} else if (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
$_SESSION['alert'] = "This image is not a valid image format!!";
echo "E-This image is not a valid image format<br>";
} else if (file_exists($upload_dir . $name)) {
$_SESSION['alert'] = "Image already exists!!";
echo "E-Image already exists<br>";
} else { // No error found! Move uploaded files
$size = getimagesize($_FILES['image']['tmp_name']);
$x = (int) $_POST['x'];
$y = (int) $_POST['y'];
$w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
$h = (int) $_POST['h'] ? $_POST['h'] : $size[1];
// path for big image
$big_image_path = $upload_dir . "big/" . $name;
// medium image path
$medium_image_path = $upload_dir . "medium/" . $name;
// small image path
$small_image_path = $upload_dir . "small/" . $name;
// check permission
if (!is_dir($upload_dir . "big/") && !is_writeable($upload_dir . "big/")) {
mkdir($upload_dir . "big/", 0777, false);
}
if (!is_dir($upload_dir . "medium/") && !is_writeable($upload_dir . "medium/")) {
mkdir($upload_dir . "medium/", 0777, false);
}
if (!is_dir($upload_dir . "small/") && !is_writeable($upload_dir . "small/")) {
mkdir($upload_dir . "small/", 0777, false);
}
// image raw data from form
$data = file_get_contents($_FILES["image"]["tmp_name"]);
// create image
$vImg = imagecreatefromstring($data);
//create big image
$dstImg = imagecreatetruecolor($nw, $nh);
imagealphablending($dstImg, false);
$trans_colour = imagecolorallocatealpha($dstImg, 0, 0, 0, 127);
imagefilledrectangle($dstImg, 0, 0, $w, $h, $trans_colour);
imagesavealpha($dstImg, true);
imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
imagepng($dstImg, $big_image_path);
//create medium thumb
$dstImg1 = imagecreatetruecolor($nw1, $nh1);
imagealphablending($dstImg1, false);
$trans_colour1 = imagecolorallocatealpha($dstImg1, 0, 0, 0, 127);
imagefilledrectangle($dstImg1, 0, 0, $w, $h, $trans_colour1);
imagesavealpha($dstImg1, true);
imagecopyresampled($dstImg1, $vImg, 0, 0, $x, $y, $nw1, $nh1, $w, $h);
imagepng($dstImg1, $medium_image_path);
// create smallest thumb
$dstImg2 = imagecreatetruecolor($nw2, $nh2);
imagealphablending($dstImg2, false);
$trans_colour2 = imagecolorallocatealpha($dstImg2, 0, 0, 0, 127);
imagefilledrectangle($dstImg2, 0, 0, $w, $h, $trans_colour2);
imagesavealpha($dstImg2, true);
imagecopyresampled($dstImg2, $vImg, 0, 0, $x, $y, $nw2, $nh2, $w, $h);
imagepng($dstImg2, $small_image_path);
/*
* Database insertion
*/
$sql = "INSERT INTO tbl_inksand_product_gallery ("
. "Product_Id,Gallery_Image_Big,Gallery_Image_Medium,Gallery_Image_Thumb,"
. "Gallery_Status,Created_By,Created_Datetime"
. ") VALUES ("
. "'{$Product_Id}','{$big_image_path}','{$medium_image_path}','{$small_image_path}',"
. "'A','$Created_By','{$time}'"
. ")";
db_query($sql);
if (db_affected_rows() == 1) {
if (imagedestroy($dstImg)) {
$_SESSION['success'] = "Image uploaded successfully.";
echo "S-Image uploaded successfully<br>";
} else {
$_SESSION['alert'] = "Image not uploaded!!";
echo "S-Image not uploaded";
}
} else {
$_SESSION['alert'] = "Error in uploading image!!";
echo "E-Error in uploading image!!";
}
}
}
}
} else {
mkdir($upload_dir, 0777);
}

Categories