Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Image resizing function problem
Old 06-28-2008, 12:12 PM Image resizing function problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
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');
    
    
?>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Last edited by pealo86; 06-28-2008 at 12:20 PM..
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 06-29-2008, 12:08 AM Re: Image resizing function problem
Extreme Talker

Posts: 238
Location: United States
Trades: 0
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, 06:48 AM Re: Image resizing function problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 06-29-2008, 06:50 AM Re: Image resizing function problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
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!
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 06-29-2008, 07:15 PM Re: Image resizing function problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to Image resizing function problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.15479 seconds with 12 queries