Reply
Resizing images code needs extension
Old 05-26-2007, 10:28 AM Resizing images code needs extension
Experienced Talker

Latest Blog Post:
Mandags spil: Gravity Pods
Posts: 33
Name: Lars N
Hi,

I have a php resizing code that resizes images and makes a thumbnail from the center of the image.

thumbnail.php
PHP Code:
<?
/**********************************************************
 * function resizejpeg:
 *
 *  = creates a resized image based on the max width
 *    specified as well as generates a thumbnail from
 *    a rectangle cut from the middle of the image.
 *
 *    @dir    = directory image is stored in
 *    @newdir = directory new image will be stored in
 *    @img    = the image name
 *    @max_w  = the max width of the resized image
 *    @max_h  = the max height of the resized image
 *    @th_w  = the width of the thumbnail
 *    @th_h  = the height of the thumbnail
 *
 **********************************************************/

function resizejpeg($dir$newdir$img$max_w$max_h$th_w$th_h)
{
   
// set destination directory
   
if (!$newdir$newdir $dir;

   
// get original images width and height
   
list($or_w$or_h$or_t) = getimagesize($dir.$img);

   
// make sure image is a jpeg
   
if ($or_t == 2) {
  
       
// obtain the image's ratio
       
$ratio = ($or_h $or_w);

       
// original image
       
$or_image imagecreatefromjpeg($dir.$img);

       
// resize image?
       
if ($or_w $max_w || $or_h $max_h) {

           
// resize by height, then width (height dominant)
           
if ($max_h $max_w) {
               
$rs_h $max_h;
               
$rs_w $rs_h $ratio;
           }
           
// resize by width, then height (width dominant)
           
else {
               
$rs_w $max_w;
               
$rs_h $ratio $rs_w;
           }

           
// copy old image to new image
           
$rs_image imagecreatetruecolor($rs_w$rs_h);
           
imagecopyresampled($rs_image$or_image0000$rs_w$rs_h$or_w$or_h);
       }
       
// image requires no resizing
       
else {
           
$rs_w $or_w;
           
$rs_h $or_h;

           
$rs_image $or_image;
       }

       
// generate resized image
       
imagejpeg($rs_image$newdir.$img100);

       
$th_image imagecreatetruecolor($th_w$th_h);

       
// cut out a rectangle from the resized image and store in thumbnail
       
$new_w = (($rs_w 2) - ($th_w 2));
       
$new_h = (($rs_h 2) - ($th_h 2));

       
imagecopyresized($th_image$rs_image00$new_w$new_h$rs_w$rs_h$rs_w$rs_h);

       
// generate thumbnail
       
imagejpeg($th_image$newdir.'thumb_'.$img100);

       return 
true;
   }

   
// Image type was not jpeg!
   
else {
       return 
false;
   }
}
?>
and
test.php
PHP Code:
<?php
require 'thumbnail.php';
$dir '/home/beta/public_html/test/';
$newdir '/home/beta/public_html/test/new/';
$img 'test.jpg';

resizejpeg($dir$newdir$img400200100100);
?>
Why I need:
  • Resizing hole directories and skip files if they already exist.
  • Naming the thumbnail the same as the image and adding it into the thumbnail directory.
My dirs are like this:
/data/media/40/ (this is one example 40 is the category and other categories are 31, etc.)
/data/thumbnails/40/

Can this be done and I'm willing to pay for this extension

- Lars
BartAfterDark is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 05-26-2007, 01:04 PM Re: Resizing images code needs extension
kids's Avatar
Ultra Talker

Posts: 271
- 1st, you need make an array of files in array of directories and do resize all of them.
- 2nd, you can use the same with upper function (resizejpeg), but change
PHP Code:
<?php
require 'thumbnail.php';
$dir '/home/beta/public_html/test/';
$newdir '/home/beta/public_html/test/new/';
$thumbdir '/home/beta/public_html/test/thumbs/';
$img 'test.jpg';

resizejpeg($dir$newdir$img400200100100);
resizejpeg($dir$thumbdir$imgxyzw);
?>
x, y, z, w replace with what you want.
kids is offline
Reply With Quote
View Public Profile Visit kids's homepage!
 
Old 05-27-2007, 09:05 AM Re: Resizing images code needs extension
Experienced Talker

Latest Blog Post:
Mandags spil: Gravity Pods
Posts: 33
Name: Lars N
And how should that help me?
BartAfterDark is offline
Reply With Quote
View Public Profile
 
Old 05-29-2007, 09:51 AM Re: Resizing images code needs extension
Average Talker

Posts: 29
Name: Amit Soni
Hello there,

Image resizing will not be very effective using PHP alone. The accurate method of image conversion/resizing is to use GDImage or ImageMagick from within PHP to create images without affecting the aspect ratio or the quality of image.

--Amit
Zitku is offline
Reply With Quote
View Public Profile Visit Zitku's homepage!
 
Reply     « Reply to Resizing images code needs extension
 

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