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
Old 10-16-2010, 04:34 PM Resizing an image:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Hi,

I'm trying to create a script that a user can upload and image, it shows on a page as a thumb and then when clicked, it opens in a lightbox window.

I have most of it sorted, but the images do not seem to be resizing...

When uploaded, they stay the original size instead of making them smaller...


The script I'm using contains this:

PHP Code:


##############################################################################################
function imageResize($width$height$target
##############################################################################################
{
  
//protect function if image sizes not set (i.e. image doesn't exist in database)
  
if((!isset($width)) || (!isset($height)))
  { 
$width '1'$height '1'; }
  
//if trying to resize an image that is much smaller than target...essentially do nothing and keep the original sizing
  
elseif(($width $target) && ($height $target))
  { }
  else
  {
    
//takes the larger size of the width and height and applies the  formula accordingly...this is so this script will work dynamically with any size image
    
if ($width $height)
    { 
$percentage = ($target $width); }
    else
    { 
$percentage = ($target $height); }

    
//gets the new value and applies the percentage, then rounds the value
    
$width round($width $percentage);
    
$height round($height $percentage);
  }
  
  
//returns the new sizes in html image tag format...
  
return "width=\"$width\" height=\"$height\"";
}
//END - imageResize 
Any ideas or am I barking up the wrong tree with this particular part of the code?
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-16-2010, 09:10 PM Re: Resizing an image:
Skilled Talker

Posts: 52
Name: Alex
Trades: 0
you should resample image stream
elf2002 is offline
Reply With Quote
View Public Profile
 
Old 10-17-2010, 04:10 AM Re: Resizing an image:
Novice Talker

Posts: 8
Name: Massimo
Trades: 0
You should use image magick with imagick libraries in php. Check if it's installed in your server, then it's easy like this:

$image_filename='my_pic.jpg';
$thumb_filename='my_thumb.jpg';

// set the thumbnail size
$x=100;
$y=100;

try {
$im = new Imagick($image_filename);
// make the thumbnail
$im->thumbnailImage($x, $y, true);
$im->writeImage($thumb_filename);
}
catch (Exception $e) {
//... put here your error catch code
}
__________________
Start a
Please login or register to view this content. Registration is FREE
at Logo Arena! Get your custom logo designed by
Please login or register to view this content. Registration is FREE
maxi-72 is offline
Reply With Quote
View Public Profile Visit maxi-72's homepage!
 
Old 10-18-2010, 04:01 AM Re: Resizing an image:
ScrapingWeb.com's Avatar
Average Talker

Posts: 25
Location: ScrapingWeb.com
Trades: 0
Try this class. Just input the path to the source image and specify the destination dimensions to get the resized image. Very handy.
__________________

Please login or register to view this content. Registration is FREE
for webmasters who need the data to get started on niche information sites. Here are
Please login or register to view this content. Registration is FREE
.
ScrapingWeb.com is offline
Reply With Quote
View Public Profile Visit ScrapingWeb.com's homepage!
 
Old 10-18-2010, 08:17 AM Re: Resizing an image:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
forgot to post the image uploder I'm using as well!

PHP Code:
##############################################################################################
function upload($user_id,$fileid)
##############################################################################################
{
  global 
$db;
  
$maxsize $_POST['MAX_FILE_SIZE'];
  if(
is_uploaded_file($_FILES['userfile']['tmp_name'][$fileid])) 
  {
        if(
$_FILES['userfile']['size'][$fileid] < $maxsize)
        {
          
$imgData addslashes(file_get_contents($_FILES['userfile']['tmp_name'][$fileid]));
          
$size getimagesize($_FILES['userfile']['tmp_name'][$fileid]);
          
          
$sql_query "SELECT * FROM user_images WHERE image_id = '999999999999999999'";
                 
          if(
mysqli_num_rows(mysqli_query($db$sql_query)) > 0)
          {
            
$sql_query "UPDATE user_images SET image_type = '{$size['mime']}', image = '{$imgData}', image_size = '{$size[3]}', image_name = '{$_FILES['userfile']['name'][$fileid]}' WHERE image_id = '$user_id'";
          }
          else
          {
            
$sql_query "INSERT INTO user_images ( image_type ,image, image_size, image_name) VALUES ('{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name'][$fileid]}')";
          }
          if(!
mysqli_query($db,$sql_query)) { echo '<div class="error">Unable to upload file</div>'; }
          else { 
$return mysqli_insert_id($db); }
        }
        else 
        {
          echo
          
'<div>File exceeds the Maximum File limit.  Skipping uploading of image.</div>
          <div>Maximum File limit is '
.$maxsize.'</div>
          <div>File '
.$_FILES['userfile']['name'][$fileid].' is '.$_FILES['userfile']['size'][$fileid].' bytes</div>
          <hr />'
;
        }
  }
  return 
$return;
}
//END - upload() 
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-18-2010, 03:25 PM Re: Resizing an image:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
This is driving me mad... is there anyway to edit the original code that I have?

When I start using other examples, it messes up all the function names on other pages...!
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-20-2010, 05:33 PM Re: Resizing an image:
Novice Talker

Posts: 5
Trades: 0
Here is an old bulletproof method I use, it's not a method but just a piece of code:

PHP Code:
//create empty image
$destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');

//create image from uploaded image $prod_img
$srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');

// the actual resize just copies the image from source to destination with new dimentions
@imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
                   or die(
'Problem In resizing');

//save file with $new_name
ImageJPEG($destimg,$new name,90)or die('Problem In saving');

//kill temp image
imagedestroy($destimg); 
__________________
Bulk image resizer and watermarker:

Please login or register to view this content. Registration is FREE
nordedata is offline
Reply With Quote
View Public Profile
 
Old 10-21-2010, 05:31 AM Re: Resizing an image:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
I get:

Parse error: syntax error, unexpected T_STRING

with the line being
PHP Code:
 ImageJPEG($destimg,$new name,90)or die('Problem In saving'); 
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-21-2010, 06:20 AM Re: Resizing an image:
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by rolda hayes View Post
I get:

Parse error: syntax error, unexpected T_STRING

with the line being
PHP Code:
 ImageJPEG($destimg,$new name,90)or die('Problem In saving'); 
I'm guessing $new name should be $new_name.
__________________

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
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-21-2010, 08:20 AM Re: Resizing an image:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Good job - that got rid of the error - NEXT..!!

PHP Code:
Warningimagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in... 
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-21-2010, 08:46 AM Re: Resizing an image:
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If you're using the code nordedata posted you'll need to define some of the variables.
__________________

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
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-22-2010, 04:01 AM Re: Resizing an image:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
@nullpointer

I think this is where I'm getting lost with this.

Would it make more sense for the images to be resized to a set size (using nordedata's code) and then use my original code to just make the thumbs appear on the pages?
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-31-2010, 05:37 AM Re: Resizing an image:
Novice Talker

Posts: 4
Name: errnoi
Trades: 0
Same problem thanks for solutions
__________________

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
errnoi1 is offline
Reply With Quote
View Public Profile Visit errnoi1's homepage!
 
Reply     « Reply to Resizing an image:
 

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