473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

resizing a image

anfetienne
424 256MB
hi.....i have this script (below #1) that is linked to another php file SimpleImage.php (#2) im trying to get it to work on my uploaded images but it keeps coming up errors.....i haven't altered anything except for:

$image->load('picture.jpg');
$image->resize(546,403);
$image->save('picture2.jpg');

I put the variables i need in there within " " instead of ' ' and i always get a error message.....am i doing something or cant this be used to the scale i require


#1
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.    include('SimpleImage.php');
  3.    $image = new SimpleImage();
  4.    $image->load('picture.jpg');
  5.    $image->resize(546,403);
  6.    $image->save('picture2.jpg');
  7. ?>
  8.  
#2
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. * This program is free software; you can redistribute it and/or 
  9. * modify it under the terms of the GNU General Public License 
  10. * as published by the Free Software Foundation; either version 2 
  11. * of the License, or (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful, 
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  15. * GNU General Public License for more details: 
  16. * http://www.gnu.org/licenses/gpl.html
  17. *
  18. */
  19.  
  20. class SimpleImage {
  21.  
  22.    var $image;
  23.    var $image_type;
  24.  
  25.    function load($filename) {
  26.       $image_info = getimagesize($filename);
  27.       $this->image_type = $image_info[2];
  28.       if( $this->image_type == IMAGETYPE_JPEG ) {
  29.          $this->image = imagecreatefromjpeg($filename);
  30.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  31.          $this->image = imagecreatefromgif($filename);
  32.       } elseif( $this->image_type == IMAGETYPE_PNG ) {
  33.          $this->image = imagecreatefrompng($filename);
  34.       }
  35.    }
  36.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  37.       if( $image_type == IMAGETYPE_JPEG ) {
  38.          imagejpeg($this->image,$filename,$compression);
  39.       } elseif( $image_type == IMAGETYPE_GIF ) {
  40.          imagegif($this->image,$filename);         
  41.       } elseif( $image_type == IMAGETYPE_PNG ) {
  42.          imagepng($this->image,$filename);
  43.       }   
  44.       if( $permissions != null) {
  45.          chmod($filename,$permissions);
  46.       }
  47.    }
  48.    function output($image_type=IMAGETYPE_JPEG) {
  49.       if( $image_type == IMAGETYPE_JPEG ) {
  50.          imagejpeg($this->image);
  51.       } elseif( $image_type == IMAGETYPE_GIF ) {
  52.          imagegif($this->image);         
  53.       } elseif( $image_type == IMAGETYPE_PNG ) {
  54.          imagepng($this->image);
  55.       }   
  56.    }
  57.    function getWidth() {
  58.       return imagesx($this->image);
  59.    }
  60.    function getHeight() {
  61.       return imagesy($this->image);
  62.    }
  63.    function resizeToHeight($height) {
  64.       $ratio = $height / $this->getHeight();
  65.       $width = $this->getWidth() * $ratio;
  66.       $this->resize($width,$height);
  67.    }
  68.    function resizeToWidth($width) {
  69.       $ratio = $width / $this->getWidth();
  70.       $height = $this->getheight() * $ratio;
  71.       $this->resize($width,$height);
  72.    }
  73.    function scale($scale) {
  74.       $width = $this->getWidth() * $scale/100;
  75.       $height = $this->getheight() * $scale/100; 
  76.       $this->resize($width,$height);
  77.    }
  78.    function resize($width,$height) {
  79.       $new_image = imagecreatetruecolor($width, $height);
  80.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  81.       $this->image = $new_image;   
  82.    }      
  83. }
  84. ?>
  85.  
  86.  
  87.  
  88.  
Mar 7 '09 #1
14 3686
Markus
6,050 Expert 4TB
The error is?
Mar 7 '09 #2
anfetienne
424 256MB
well.....the error has stopped but it said that this was invalid

function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);

and it wasn't detecting the files i was pointing to with my variable....this is

$resizeM = "upload/imgtemp/m/$random_digit/$filename";

include('SimpleImage.php');
$image = new SimpleImage();
$image->load("$resizeM");
$image->resize(250,400);
$image->save("$resizeM");
Mar 7 '09 #3
anfetienne
424 256MB
thats the changes ive made
Mar 7 '09 #4
hoopy
88
Looks like a problem with the path try:

Expand|Select|Wrap|Line Numbers
  1. echo $resizeM;
And see what it outputs.

Also where is that script in relation to the uploads/ directory?
Mar 8 '09 #5
Markus
6,050 Expert 4TB
The error has now stopped? Is the image resizing working now then?
Mar 8 '09 #6
anfetienne
424 256MB
no it isn't resizing but it isn't giving me any errors.....its weird.....when i echo $resizeM i get the full location to the images that need resizing.

i'm going to think abaout this one and post again soon
Mar 9 '09 #7
Markus
6,050 Expert 4TB
Do you have error reporting on?

Nothing jumps out at me, but could you be uploading an image that isn't a JPEG, PNG or GIF?
Mar 9 '09 #8
anfetienne
424 256MB
error reporting is on and i have a set of test jpeg's that i am using to upload.

it's strange how it comes up with no errors at all but doesnt resize anything. I can't seem to figure this out

would it be better or easier to try resize the images during the upload process?
Mar 10 '09 #9
anfetienne
424 256MB
this is the coding i made to upload my files with.....can it be adapted to resize the images on upload?

Expand|Select|Wrap|Line Numbers
  1. <?
  2. while(list($key,$value) = each($_FILES['images']['name']))
  3.       {
  4.          if(!empty($value))
  5.          {
  6.             $filename = $value;
  7.                $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  8.  
  9.                $add = "upload/imgtemp/m/$random_digit/$filename";
  10.                $tempFLDR = "upload/imgtemp/t/$random_digit/$filename";
  11.                        //echo $_FILES['images']['type'][$key];
  12.               // echo "<br>";
  13.                copy($_FILES['images']['tmp_name'][$key], $add);
  14.                chmod("$add",0777);
  15.                copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  16.                chmod("$tempFLDR",0777);
  17.  
  18. $folderPath="upload/$random_digit/";
  19.          }
  20.       }
  21. ?>
  22.  
Mar 10 '09 #10
anfetienne
424 256MB
wooohoooo i have got it to resize lol but errmmmm if i upload 1 image then it resizes fine but if i upload more than one image it only resizes the last image.

why does it do this....here is the code i have that is working for me

Expand|Select|Wrap|Line Numbers
  1. <?
  2. while(list($key,$value) = each($_FILES['images']['name']))
  3.         {
  4.             if(!empty($value))
  5.             {
  6.                 $filename = $value;
  7.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  8.  
  9.                     $add = "upload/imgtemp/m/$random_digit/$filename";
  10.                     $tempFLDR = "upload/imgtemp/t/$random_digit/$filename";
  11.                        //echo $_FILES['images']['type'][$key];
  12.                  // echo "<br>";
  13.                     copy($_FILES['images']['tmp_name'][$key], $add);
  14.                     chmod("$add",0777);
  15.                     copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  16.                     chmod("$tempFLDR",0777);
  17.  
  18. $folderPath="upload/$random_digit/";
  19.             }
  20.         }
  21. include('SimpleImage.php');
  22. $image = new SimpleImage();
  23. $image->load("$add");
  24. $image->resize(250,400);
  25. $image->save("$add"); 
  26. ?>
  27.  
Mar 10 '09 #11
Markus
6,050 Expert 4TB
That's because your image manipulation happens outside of the while loop, meaning only the last image's path will be read. Move the include to the beginning of the file, and move the method calls to the end of the while loop.
Mar 10 '09 #12
anfetienne
424 256MB
like this? im still a newbie....sorry for the hassle

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include('SimpleImage.php');
  3. while(list($key,$value) = each($_FILES['images']['name']))
  4.         {
  5.             if(!empty($value))
  6.             {
  7.                 $filename = $value;
  8.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  9.  
  10.                     $add = "upload/imgtemp/m/$random_digit/$filename";
  11.                     $tempFLDR = "upload/imgtemp/t/$random_digit/$filename";
  12.                        //echo $_FILES['images']['type'][$key];
  13.                  // echo "<br>";
  14.                     copy($_FILES['images']['tmp_name'][$key], $add);
  15.                     chmod("$add",0777);
  16.                     copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  17.                     chmod("$tempFLDR",0777);
  18.  
  19. $folderPath="upload/$random_digit/";
  20.             }
  21. $image = new SimpleImage();
  22. $image->load("$add");
  23. $image->resize(546,403);
  24. $image->save("$add"); 
  25.         }
  26.  
  27. ?>
  28.  
Mar 10 '09 #13
anfetienne
424 256MB
Thanks Markus!!!!! it works like a charm...i'll be back with a link once the finished product is done.

Thanks again
Mar 10 '09 #14
Markus
6,050 Expert 4TB
@anfetienne
Coolios, and you're welcome.

You don't need to recreate the object ($x = new x;) in the while loop - that'll use up more resources. So, just create the object after you include the class.
Mar 11 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alex Hopson | last post by:
I'm using the code below to loop through some images and resize each image twice, once to create a thumbnail and once to create a small image. The page stops loading around the 38th image out of...
2
by: Clyde Ellul | last post by:
Hi there. I need to write a simple program that reads a GIF image from an input stream, resizes it, then writes it back to an output stream in the same format (GIF). (JPEG input/output is good...
10
by: riki | last post by:
Hi, i have a big problem...i'm using one jscript for resizing of all of my pics in popUp...in main html i'm having many little pics and clicking on them they open in popUp and resize to larger...
5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
1
by: Ron Vecchi | last post by:
I am using asp.net to upload an image and then perform resizing on it and saving the different sizes to file. The resized images were coming up and being displayed in the bowser fine but the image...
10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
6
by: tomasio | last post by:
Dear NG, years have passed and I am still more designer than programmer. I build a new version of my website which has a few nasty bugs, especially on my startpage: Resizing text brakes the...
8
by: berkshire | last post by:
Hi, Anyone know of a script out there that can resize images and not sacrifice image quality? I've been using phpthumb (http://phpthumb.sourceforge.net/) but when compared to an image resized...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.