Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.