SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Resizing a single file

    Hi,
    I have a site where pictures are shown for visitors. The admin of the site wants to show one of the pictures on the front page - which he can choose from the list of the pictures available.

    I want to resize that picture (say, it has variable $MainPagePic) and show it on the mainpage. So - no saving the file to any other file... just resize and post it. I also want to check the max width and height (which ever comes first)

    example:
    max width: 300 // max height: 300
    photo1.jpg (w: 600 h: 400) >> resize >> <img src='photo1.jpg'> (w: 300 h: 200).
    photo2.jpg (w: 400 h: 600) >> resize >> <img src='photo2.jpg'> (w: 200 h: 300).

    How can this be done easily?

    Any help is appreciated.
    Jazz

  2. #2
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    Code:
    $file = $imgFile;
    
    $nameArray = split(".jpg", $file);
    $name = $nameArray[0];
    
    if(!file_exists($name."_thumbnail.jpg"))
    {
    
    $img1 = getimagesize($file);
        if ($img1[2] == 2) { 
    	
    
    
    $img = ImageCreateFromJpeg($file); 
    $img_size = getimagesize($file);
    $img_w = $img_size[0];
    $img_h = $img_size[1];
    $destimg_w = 300;
    $destimg_h = 300;
    
    
    $mlt_w = $img_w / $destimg_w ;
    $mlt_h = $img_h /$destimg_h ;
    
    if($mlt_w < $mlt_h)
      {
      
      $re_img_h = $img_h / $mlt_h;
      $re_img_w = $img_w / $mlt_h;
      
      }
      else
      {
       $re_img_h = $img_h / $mlt_w;
      $re_img_w = $img_w / $mlt_w;
      
      }
    
    $img_resized = imagecreatetruecolor( $re_img_w,  $re_img_h);
    imagecopyresampled($img_resized, $img, 0, 0, 0, 0, $re_img_w, $re_img_h, $img_w,$img_h);
    ImageJPEG($img_resized, $name."_thumbnail.jpg", "80");
    	}
    }
    
    
    
    $imgFile = $name."_thumbnail.jpg";
    This will create a copy of your current file called oldfile_thumbnail.jpg with 300px width and hight keeping constrains.

    hope this will do the job

    grtz Martijn Schot

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The file won't be saved anywhere, will it? (I don't want it to)
    I just want to resize it and print it on screen.

    PS: thanx for the code

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have your admin put this on his page with the name of your php file and the path to your image:
    Code:
    <img src="atest3.php?img=../images/105_0532.jpg" />
    Then try this:
    PHP Code:
    $fname $_GET['img'];

    $copy imagecreatefromjpeg($fname);
    $oldWidth imagesx($copy);
    $oldHeight imagesy($copy);

    $max 300;

    if(
    $oldWidth $oldHeight)
    {
        
    $newWidth 300;  //If oldWidth was 600, then newWidth  
                 //is 300/600 smaller and that fraction needs to be 
                 //multiplied by the oldHeight to get the new height

        
    $newHeight = (300/$oldWidth) * $oldHeight;  
    }
    else
    {
        
    $newHeight 300;
        
    $newWidth = (300/$oldHeight) * $oldWidth;
    }

    $newImg imagecreatetruecolor($newWidth$newHeight);
    imagecopyresampled($newImg$copy0,0,0,0$newWidth$newHeight$oldWidth$oldHeight); 

    header('Content-Type: image/jpeg');
    imagejpeg($newImg);

    imagedestroy($copy); 
    imagedestroy($newImg); 
    Edit:

    Added two lines at the end.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, you need to to check to see if your server has the gd library enabled and that it is version 2.0+. Do this:

    phpinfo();

    and look under the 'gd' heading.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •