Jump to content

need help in imagecopyresampled


doforumda

Recommended Posts

hi

 

i am trying to upload images and resize them to thumbnail size through imagecopyresampled. I allowing four kinds of images gif,jpg,jpeg and png. the problem is imagecopyresampled only works for jpg not for others. my code is below please help me out where i am making mistake

<?php
include("getExtension.php");
$filename = 'banner.gif';
$ext = getExtension($filename);
$ext = strtolower($ext);

$jpg = false;
$png = false;
$gif = false;
// Content type
if($ext=='jpeg' || $ext == 'jpg') { 
$jpg = true;
header('Content-type: image/jpeg');
}
if($ext=='png') { 
$png = true;
header('Content-type: image/png');
}
if($ext=='gif') {
$gif = true;
header('Content-type: image/gif');
}

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = 80;
$new_height = 80;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
if($jpg==true) {
imagejpeg($image_p, $save, 100);
}
if($png==true) {
imagepng($image_p, $save, 100);
}
if($gif==true) {
imagegif($image_p, $save, 100);
}
?>

 

above code displays this error

The image “http://localhost/imageResizer/image_resizer.php” cannot be displayed, because it contains errors.

Link to comment
https://forums.phpfreaks.com/topic/198082-need-help-in-imagecopyresampled/
Share on other sites

Of course your code will only work for jpg images. Look at this line:

 

$image = imagecreatefromjpeg($filename); // can't use this on gif or png!

 

You need to to check the image type, if it's gif use imagecreatefromgif() or if it's a png then use imagecreatefrompng()

now it is working for jpg and gif but not for png

 

i modify my code and add these

if($jpg==true) {
$image = imagecreatefromjpeg($filename);
}
if($png==true) {
$image = imagecreatefrompng($filename);
}
if($gif==true) {
$image = imagecreatefromgif($filename);
}

now it just display this

http://localhost/imageResizer/image_resizer.php

 

no errors are displayed

 

Comment out imagepng/jpg/gif lines including header lines, and add error reporting. It will prevent the image from parsing, and send you an actual error. Obviously you can't see one if there is no domain for text to be displayed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.