Reply
Image resizing function problem
Old 06-28-2008, 11:12 AM Image resizing function problem
KkillgasmM's Avatar
Ultra Talker

Posts: 352
Name: El Phantasmo
Location: England, north west
I turned an image resizing script I coded previously; into a function so that I can re use the code when I like.

The problem is, it doesn't really seem to do anything anymore! Im thinking, do I need to 'return' something at the end? I dont see why I would though?

Here is my code!
PHP Code:
<?php

function imgResize ($fieldName$width$maxHeight)
    {
        
// temporary file created by php
        
$imgUpload $_FILES[$fieldName]['tmp_name'];
        
        
// create an image from it to be resized
        
$imgSrc imagecreatefromjpeg ($imgUpload);
        
        
// Capture the original size of the uploaded image
        
$size = array (getimagesize ($imgUpload));
        
$width $size[0];
        
$height $size[1];
        
        
// define new dimensions
        
$newWidth $width;
        
$newHeight = ($height $width) * $newWidth;
                
        
// height must be no more than $maxHeight
        
if($newHeight $maxHeight)
            {        
                
$newHeight $maxHeight;
                
$newWidth = ($width $height) * $newHeight;
            }
        
        
// create true color image
        
$imgDest imagecreatetruecolor ($newWidth$newHeight);
        
        
// resize $imgSrc and turn into $imgDest
        
imagecopyresampled ($imgDest$imgSrc0000$newWidth$newHeight$width$height);
        
        
// write to disk
        
$destination '../mattPealingDesign/uploadImg/uploadedImage.jpg';
        
        if (
imagejpeg ($imgDest$destination100))
            {
                echo 
'Upload Successful';
            }
        
        else
            {
                echo 
'error';
            }
    }
I should also point out that Im calling the function in another script with:
PHP Code:
<?php
    
    
include "../script/php/imgResize.php";
    
    
imgResize ('portImg''800''1000');
    
    
?>
__________________
http://www.inspindesign.co.uk

Last edited by KkillgasmM : 06-28-2008 at 11:20 AM.
KkillgasmM is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 06-28-2008, 11:08 PM Re: Image resizing function problem
Extreme Talker

Posts: 235
Location: United States
It doesn't do anything? Does it echo one of the two messages you have in the function, or is it failing silently? If it's failing silently, you may want to enable error reporting or set it to E_ALL.

The function seems to both resize the image and upload it, despite its name, so is it possible that you are uploading the image twice somewhere else in your code? Perhaps first as a resized image, then later overwriting it as the original image?

You don't need to return anything for a function; whether you do or not is up to you and what you need.

Also, it's good practice to use imagedestroy() on your image handlers once you are done with them. It's not strictly necessary in this case though.
frost is offline
Reply With Quote
View Public Profile
 
Old 06-29-2008, 05:48 AM Re: Image resizing function problem
KkillgasmM's Avatar
Ultra Talker

Posts: 352
Name: El Phantasmo
Location: England, north west
Yeah it seems to fail silently; as in none of the two error messages are displayed. I'll try changing the error reporting!

Thanks I did used to have imagedestroy () in the script but I must have removed it at some point! I'll have to get it back in there
__________________
http://www.inspindesign.co.uk
KkillgasmM is offline
Reply With Quote
View Public Profile
 
Old 06-29-2008, 05:50 AM Re: Image resizing function problem
KkillgasmM's Avatar
Ultra Talker

Posts: 352
Name: El Phantasmo
Location: England, north west
Actually I've just spotted an error! I've used $width as a variable for two different things.

I dont know whether that would be causing the problem, but I'll soon find out!
__________________
http://www.inspindesign.co.uk
KkillgasmM is offline
Reply With Quote
View Public Profile
 
Old 06-29-2008, 06:15 PM Re: Image resizing function problem
KkillgasmM's Avatar
Ultra Talker

Posts: 352
Name: El Phantasmo
Location: England, north west
I had several code issues which Ive fixed:

PHP Code:
<?php

function imgResize ($fieldName$width$maxHeight)
    {
        
// temporary file created by php
        
$imgUpload $_FILES[$fieldName]['tmp_name'];
        
        
// create an image from it to be resized
        
$imgSrc imagecreatefromjpeg ($imgUpload);
        
        
// Capture the original size of the uploaded image
        
$size = array (getimagesize ($imgUpload));
        
$origWidth $size[0];
        
$origHeight $size[1];
        
        
// define new dimensions
        
$newWidth $width;
        
$newHeight = ($origHeight $origWidth) * $newWidth;
                
        
// height must be no more than $maxHeight
        
if($newHeight $maxHeight)
            {        
                
$newHeight $maxHeight;
                
$newWidth = ($origWidth $origHeight) * $newHeight;
            }
        
        
// create true color image
        
echo '<p>' $newWidth ' ' $newHeight '</p>';
        
        
$imgDest imagecreatetruecolor ($newWidth$newHeight);
        
        
// resize $imgSrc and turn into $imgDest
        
imagecopyresampled ($imgDest$imgSrc0000$newWidth$newHeight$origWidth$origHeight);
        
        
// write to disk
        
$destination '../uploadImg/uploadedImage.jpg';
        
        if (
imagejpeg ($imgDest$destination100))
            {
                echo 
'Upload Successful';
            }
        
        else
            {
                echo 
'error';
            }
    }

?>
But it still doesn't work! Here is the error generated:

Quote:
Notice: Undefined offset: 1 in C:\xampp\htdocs\xampp\mattPealingDesign\script\php \imgResize.php on line 14

Fatal error: Unsupported operand types in C:\xampp\htdocs\xampp\mattPealingDesign\script\php \imgResize.php on line 18
__________________
http://www.inspindesign.co.uk
KkillgasmM is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Image resizing function problem
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15344 seconds with 13 queries