Reputation: 19
Hello i have an issue about php. In my php files i have 1 part for image upload. It also translates and uploads the image I uploaded in the same small size. But the reduction does not work for the .png extension while working for the .jpg extension.
$uploaddir = "storage/products/screenshot/"; //Screenshot upload directory
$valid_formats = array("jpg","jpeg","png"); // Valid image formats
$countScreen = 0;
foreach ($_FILES['item_screen']['name'] as $name => $value) {
$filename = stripslashes($_FILES['item_screen']['name'][$name]);
$size = filesize($_FILES['item_screen']['tmp_name'][$name]);
//Convert extension into a lower case format
$ext = getExtension($filename);
$ext = strtolower($ext);
//File extension check
if (in_array($ext, $valid_formats)) {
if($ext=="jpg" || $ext=="jpeg" )
{
$uploadedfile = $_FILES['item_screen']['tmp_name'][$name];
$src = @imagecreatefromjpeg($uploadedfile);
}
else if($ext=="png")
{
$uploadedfile = $_FILES['item_screen']['tmp_name'];
$src = @imagecreatefrompng($uploadedfile);
}
else
{
$src = @imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=800;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=222;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
@imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
@imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$random1 = rand(9999, 100000);
$random2 = rand(9999, 200000);
$image_name = $first_title . '_' . $random1 . $random2 . '.' . $ext;
$image_name1 = 'small_' .$first_title . '_' . $random1 . $random2 . '.' . $ext;
//$newname = $uploaddir . $image_name;
$filename = $uploaddir . $image_name;
$filename1 = $uploaddir . $image_name1;
$pic_name = $image_name1;
@imagejpeg($tmp,$filename,100);
@imagejpeg($tmp1,$filename1,100);
@imagedestroy($src);
@imagedestroy($tmp);
@imagedestroy($tmp1);
//Moving file to uploads folder
if (!move_uploaded_file($_FILES['item_screen']['tmp_name'][$name], $filename)) {
$errors[]['message'] = $lang['ERROR_UPLOAD_IMG'];
}
if ($countScreen == 0)
$image_name2 = $image_name;
elseif ($countScreen >= 1)
$image_name2 = $image_name2 . "," . $image_name;
} else {
$errors[]['message'] = $lang['ONLY_JPG_ALLOW'];
}
$countScreen++;
}
Github gist: https://gist.github.com/barangndz/e911ec01494bcfc594145c9edf3611d6
Upvotes: 0
Views: 53
Reputation:
I think you need to try with this method:
if($ext=="jpg" || $ext=="jpeg" || $ext=="png" )
{
$uploadedfile = $_FILES['item_screen']['tmp_name'][$name];
$src = @imagecreatefromjpeg($uploadedfile);
}
else
{
$src = @imagecreatefromgif($uploadedfile);
}
Upvotes: 1