Jump to content

php resize image then crop image problem


zac1987

Recommended Posts

I want to reduce a picture size from 600px * 500px to 60px * 50px size, then crop it become 50px *50px. I have two groups of codes, 1 is to reduce the size of image, other 1 is to crop the image. The problem is they works separately, how to combine this two groups of codes to make them work together? Below is my codes :

 

<?php
//codes of group A - Reduce the size of image from 600px * 500px to 60px * 50px   
    $save2 = "images/users/" . $image_name_2; //This is the new file you saving
    list($width2, $height2) = getimagesize($file) ; 
    $modwidth2 = 50; 
    $diff2 = $width2 / $modwidth2;
    $modheight2 = $height2 / $diff2; 
    $tn2 = imagecreatetruecolor($modwidth2, $modheight2) ; 
    $image2 = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2) ; 
    imagejpeg($tn2, $save2, 100) ; 

//codes of group B - Crop the image from 60px * 50px to 50px * 50px 
    $save3 = "images/users/" . $image_name_3;  
    list($width3, $height3) = getimagesize($file) ; 
    $modwidth3 = 60;  
    $diff3 = $width3 / $modwidth3;
    $modheight3 = $height3 / $diff3; 

    $left = 0; 
    $top = 0;

    $cropwidth = 50; //thumb size
    $cropheight = 50;

    $tn3 = imagecreatetruecolor($cropwidth, $cropheight) ; 
    $image3 = imagecreatefromjpeg($file) ; 
    imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ; 
    imagejpeg($tn3, $save3, 100) ; //save the cropped image
?>

As you can see from 2 groups of codes above, 1st group resize the pic then save it to a folder. 2nd group of codes crop the pic then save it into the folder too. My question is ... After 1st group of codes resize the picture, is it necessary to save it into folder before I can crop it? If it is necessary, then I need to write new lines of codes to retrieve the resized pic from the folder for 2nd group of codes to crop it? If it is not necessary, after resizing the pic, how do I pass the pic to 2nd group of codes to crop it?

php image-resizing

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.