TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   cropping images ( this style ) [REQUEST] (http://www.talkphp.com/general/2808-cropping-images-style-request.html)

Orc 05-15-2008 04:00 PM

cropping images ( this style ) [REQUEST]
 
how do I do this:
http://garrysmod.com/challenge/image...30&w=125&h=250

I made a crop image which centers itself from the corridinates by cropped images, and dividing by 2 but.. how do I achieve this effect, by the way, go ahead and increase the w and h url vars to see more.

Orc 05-16-2008 02:35 AM

[bump] 12346 vvvv

Kalle 05-17-2008 05:55 AM

I did a quick thumbnail function here to show you how simple it really is, it might not be working as im pretty tired and I was about to go to bed ;)

PHP Code:

<?php
    
/** Assuming $source is an image resource to the image we need to alter */

    /** Max width + height */
    
$width 100;
    
$height 100;

    
$factor 1;

    if(
imagesx($source) > $width)
    {
        
$factor = ($width imagesx($source));
    }

    if((
imagesy($source) * $factor) > $height)
    {
        
$factor = ($height imagesy($source));
    }

    
$width floor(imagesx($source) * $factor);
    
$height floor(imagesy($source) * $factor);

    
$thumbnail imagecreatetruecolor($width$height);
    
$bg imagecolorallocate($thumbnail255255255);
    
imagefilledrectangle($thumbnail00, (imagesx($thumbnail) - 1), (imagesy($thumbnail) - 1), $bg);

    
imagecopyresampled($thumbnail$source0000$width$heightimagesx($source), imagesy($source));
    
imagedestroy($source);

    
$source NULL;

    
/** $thumbnail is now the generated thumbnail */
?>


Orc 05-18-2008 10:26 PM

Quote:

Originally Posted by Kalle (Post 14698)
I did a quick thumbnail function here to show you how simple it really is, it might not be working as im pretty tired and I was about to go to bed ;)

PHP Code:

<?php
    
/** Assuming $source is an image resource to the image we need to alter */

    /** Max width + height */
    
$width 100;
    
$height 100;

    
$factor 1;

    if(
imagesx($source) > $width)
    {
        
$factor = ($width imagesx($source));
    }

    if((
imagesy($source $factor) > $height)
    {
        
$factor = ($height imagesy($source));
    }

    
$width floor(imagesx($source) * $factor);
    
$height floor(imagesy($source) * $factor);

    
$thumbnail imagecreatefromtruecolor($width$height);
    
$bg imagecolorallocate($thumbnail255255255);
    
imagefilledrectangle($thumbnail00, (imagesx($thumbnail) - 1), (imagesy($thumbnail) - 1), $bg);

    
imagecopyresampled($thumbnail$source0000$width$heightimagesx($source), imagesy($source));
    
imagedestroy($source);

    
$source NULL;

    
/** $thumbnail is now the generated thumbnail */
?>


Didn't work at all :P

Kalle 05-19-2008 12:47 AM

Hmm I fixed the code, but I see it wasn't what you wanted in first place, but thats what you get out of being up late >.<

Orc 05-19-2008 06:29 AM

Quote:

Originally Posted by Kalle (Post 14727)
Hmm I fixed the code, but I see it wasn't what you wanted in first place, but thats what you get out of being up late >.<

xD, it's alright. I'll figure it out some how.

delayedinsanity 05-19-2008 08:35 AM

PHP: imagecopyresized - Manual

Will do what you want, look specifically at SRC_X and SRC_Y -- you can tell it what coordinates you want to start the copy at, effectively creating a cropped area.
-m

Orc 05-19-2008 08:37 AM

Quote:

Originally Posted by delayedinsanity (Post 14731)
PHP: imagecopyresized - Manual

Will do what you want, look specifically at SRC_X and SRC_Y -- you can tell it what coordinates you want to start the copy at, effectively creating a cropped area.
-m

I use resampled
:P

delayedinsanity 05-19-2008 04:37 PM

Okay, but it can still do it, and in this case if all you want to do is create a cropped area you don't need to use resampled, because you're not resizing. You're just chopping some off.
-m

edit: I just looked closer at your example. It's not cropping like I originally thought, it's just resizing. So that should be easy? Let me try something here...

delayedinsanity 05-19-2008 04:59 PM

Okay, so theirs isn't just resizing, it's cropping too, based on what size you enter... so this is pretty close, all mine does is resize it, but I just woke up. I think they're probably using some kind of math to figure out a crop area based on the ratio of the size entered. I didn't tinker with that at all. Also i'm not sure if you can dynamically display the new image without saving it as an actual physical file somewhere. If you can, I'd love to know how, if anybody knows?

PHP Code:

<?php
function thumbnail ($szFile$intNewWidth$intNewHeight) {

    
$aImg getimagesize($szFile);
        
$intWidth $aImg[0];
        
$intHeight $aImg[1];
        
$szFileType $aImg['mime'];
    unset(
$aImg);

    switch (
$szFileType) {
        case 
"image/pjpeg": case "image/jpeg"$imgNew imagecreatefromjpeg($szFile); break;
        case 
"image/x-png": case "image/png":  $imgNew imagecreatefrompng($szFile); break;
        case 
"image/gif"$imgNew imagecreatefromgif($szFile); break;
        default: return 
false;
    }

    
$imgResized imagecreatetruecolor($intNewWidth$intNewHeight);
    
imagecopyresampled($imgResized$imgNew0000$intNewWidth$intNewHeight$intWidth$intHeight);
    if (
imagejpeg($imgResized"temp/temp.jpg"100)) {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
true;
    } else {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
false;
    }

// thumbnail()

$file "path/to/your.jpg";
thumbnail($file$_GET['w'], $_GET['h']);
?>

<img src="temp/temp.jpg" />

-m

Orc 05-19-2008 09:38 PM

Quote:

Originally Posted by delayedinsanity (Post 14741)
Okay, so theirs isn't just resizing, it's cropping too, based on what size you enter... so this is pretty close, all mine does is resize it, but I just woke up. I think they're probably using some kind of math to figure out a crop area based on the ratio of the size entered. I didn't tinker with that at all. Also i'm not sure if you can dynamically display the new image without saving it as an actual physical file somewhere. If you can, I'd love to know how, if anybody knows?

PHP Code:

<?php
function thumbnail ($szFile$intNewWidth$intNewHeight) {

    
$aImg getimagesize($szFile);
        
$intWidth $aImg[0];
        
$intHeight $aImg[1];
        
$szFileType $aImg['mime'];
    unset(
$aImg);

    switch (
$szFileType) {
        case 
"image/pjpeg": case "image/jpeg"$imgNew imagecreatefromjpeg($szFile); break;
        case 
"image/x-png": case "image/png":  $imgNew imagecreatefrompng($szFile); break;
        case 
"image/gif"$imgNew imagecreatefromgif($szFile); break;
        default: return 
false;
    }

    
$imgResized imagecreatetruecolor($intNewWidth$intNewHeight);
    
imagecopyresampled($imgResized$imgNew0000$intNewWidth$intNewHeight$intWidth$intHeight);
    if (
imagejpeg($imgResized"temp/temp.jpg"100)) {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
true;
    } else {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
false;
    }

// thumbnail()

$file "path/to/your.jpg";
thumbnail($file$_GET['w'], $_GET['h']);
?>

<img src="temp/temp.jpg" />

-m

I have no idea, I know the person who made the site though, all he told me was he kept the aspect ratio the same, though I did that, and it still looks like shit. :P his is better. and he said to do it, was just math. -_- ugh.

delayedinsanity 05-19-2008 10:29 PM

If you know the person, tell him to cough up the code so you can dissect it and learn!
-m

Orc 05-19-2008 10:31 PM

Quote:

Originally Posted by delayedinsanity (Post 14746)
If you know the person, tell him to cough up the code so you can dissect it and learn!
-m

Well alright, but hes not one to really give up his code. ;P

He has his own forums:
Facepunch Studios (Forums)

though I warn you, theres idiots about xD

j4v1 05-21-2008 03:33 PM

[quote=delayedinsanity;14731]PHP: imagecopyresized - Manual

Will do what you want, look specifically at SRC_X and SRC_Y -- you can tell it what coordinates you want to start the copy at, effectively creating a cropped area.
-m[/QUoute]
************************************************** ********
One quick question. I'm trying to do the same thing. I'd like to start my cropping from a particular area/coordinates.

Below is the sample code. Also, if you follow the link to the original gif and then in your favorite editor crop the image so that its at the following dimension 438x519 from the center. I took the following pixels (left 0, Right 119, Top 476, Bottom 204). This above result is what I'm trying to get the following code to produce.

Any assistance again you may provide would truly be appreciated.

Thank you,
j4v1

PHP Code:

<?php
cropImage
(500460'http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif''gif''test500x460.gif');
function 
cropImage($nw$nh$source$stype$dest) {
$size getimagesize($source);
$w $size[0];
$h $size[1];
switch(
$stype) {
case 
'gif':
$simg imagecreatefromgif($source);
break;
case 
'jpg':
$simg imagecreatefromjpeg($source);
break;
case 
'png':
$simg imagecreatefrompng($source);
break;
}
$dimg imagecreatetruecolor($nw$nh);
$wm $w/$nw;
$hm $h/$nh;
$h_height $nh/2;
$w_height $nw/2;
if(
$w $h) {
$adjusted_width $w $hm;
$half_width $adjusted_width 2;
$int_width $half_width $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif((
$w $h) || ($w == $h)) {
$adjusted_height $h $wm;
$half_height $adjusted_height 2;
$int_height $half_height $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
?>



All times are GMT. The time now is 10:12 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0