I am trying to cut an image for small pieces from database. My code here:
$image = imagecreatefromjpeg($filename);
$width = $row['width'];
$height = $row['height'];
$new_width = 480;
$new_height = 360;
$offset_x = (int)(round($height*360/$width));//100
$offset_y = 0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
imagejpeg($new_image,$newimg);
But in the line $offset_x, the (int)(round($height*360/$width)) is not working. If I put an intval, like 100, all the images will cut well, but the offset position is not willing. So how to true string to int?
BTW: In this case, I will call and cut image from database in a foreach, should I unset something after this code in the foreach?
You rarely need to convert strings to numbers in PHP, it does that implicitely pretty well. For example, your code works just fine without any added conversions:
http://codepad.viper-7.com/LJEXIF
The problem you might be having, if it’s not giving you the right result, is the priority of operations. Your code,
Is the equivalent of
It will first divide
360by$width, then multiply the result by$height. If that’s what you wanted, then your problem lies elsewhere. You should try and reproduce your problem with test variables on http://codepad.viper-7.com and post it back here.